一.学习的内容
1,图表辅助元素
(1)设置坐标轴的标签、刻度范围和刻度标签
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
y1,y2 = np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
#s=设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.title("2020080603043")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
y1,y2 = np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
#s=设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5,x.max() *1.5)
plt.xticks([-np.pi,-np.pi/2 ,0, np.pi/2,np.pi],[r'$-\pi$' ,r'$-pi/2$' ,r'$0$', r'$\pi/2$' ,r'$\pi$'])
plt.title("2020080603043")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
y1,y2 = np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
#s=设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5,x.max() *1.5)
plt.xticks([-np.pi,-np.pi/2 ,0, np.pi/2,np.pi],[r'$-\pi$' ,r'$-pi/2$' ,r'$0$', r'$\pi/2$' ,r'$\pi$'])
#添加标题
plt.title("正弦曲线和余弦曲线")
plt.show()
(2)添加标题和图例
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
y1,y2 = np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
#s=设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5,x.max() *1.5)
plt.xticks([-np.pi,-np.pi/2 ,0, np.pi/2,np.pi],[r'$-\pi$' ,r'$-pi/2$' ,r'$0$', r'$\pi/2$' ,r'$\pi$'])
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加图例
plt.legend(['正弦','余弦'] , shadow =True ,fancybox =True)
plt.show()
(3)实例:支付宝月账单
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
kinds=['购物','人情来往','餐饮美食','通信物流','生活日用','交通出行','休闲娱乐','其他']
money_scale=[800/3000, 100/3000,1000/3000,200/3000,300/3000,200/3000,200/3000,200/3000]
dev_position=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
plt.pie(money_scale,labels=kinds,autopct='%3.1f%%', shadow=True, explode=dev_position,startangle=90)
#添加标题
plt.title('支付宝月账单报告')
#添加图例
plt.legend(kinds, loc='upper right',bbox_to_anchor=[1.3,1.1])
plt.show()
(4)显示网格
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
y1,y2 = np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5,x.max() *1.5)
plt.xticks([-np.pi,-np.pi/2 ,0, np.pi/2,np.pi],[r'$-\pi$' ,r'$-pi/2$' ,r'$0$', r'$\pi/2$' ,r'$\pi$'])
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加图例
plt.legend(['正弦','余弦'] , shadow =True ,fancybox =True)
# 显示网格
plt.grid(b= True , axis='y' ,linewidth=0.3)
plt.show()
(5)实例3
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x_speed =np.arange(10,210,10)
y_distance = np.array([0.5,2.0,4.4,7.9,12.3,17.7,24.1,31.5,39.9,49.2,59.5,
70.8,83.3,96.4,110.7,126.0,142.2,159.4,177.6,196.8])
plt.scatter(x_speed,y_distance, s=50 , alpha=0.9 ,linewidths=0.3)
#设置x轴的标签、刻度标签
plt.xlabel('速度(km/h)')
plt.ylabel('制动距离')
# 显示网格
plt.grid(b= True , axis='y' ,linewidth=0.3)
plt.title("2020080603043")
plt.show()
(6)添加参考线、添加参考区域
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
y1,y2 = np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5,x.max() *1.5)
plt.xticks([-np.pi,-np.pi/2 ,0, np.pi/2,np.pi],[r'$-\pi$' ,r'$-pi/2$' ,r'$0$', r'$\pi/2$' ,r'$\pi$'])
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加图例
plt.legend(['正弦','余弦'] , shadow =True ,fancybox =True)
# 显示网格
plt.grid(b= True , axis='y' ,linewidth=0.3)
#添加参考线
plt.axvline(x=0 ,linestyle='--')
plt.axhline(y=0 ,linestyle ='--')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
y1,y2 = np.sin(x),np.cos(x)
plt.plot(x,y1,x,y2)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5,x.max() *1.5)
plt.xticks([-np.pi,-np.pi/2 ,0, np.pi/2,np.pi],[r'$-\pi$' ,r'$-pi/2$' ,r'$0$', r'$\pi/2$' ,r'$\pi$'])
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加图例
plt.legend(['正弦','余弦'] , shadow =True ,fancybox =True)
# 显示网格
plt.grid(b= True , axis='y' ,linewidth=0.3)
#添加参考线
plt.axvline(x=0 ,linestyle='--')
plt.axhline(y=0 ,linestyle ='--')
#添加参考区域
plt.axvspan(xmin=0.5 ,xmax=2.0 ,alpha=0.3)
plt.axhspan(ymin=0.5 ,ymax=1.0 ,alpha=0.3)
plt.show()
(7)实例4
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
men_means =( 90.5,89.5,88.7,88.5,85.2,86.6)
women_means =(92.7,87.0,90.5,85.0,89.5,89.8)
ind = np.arange(len(men_means))#每组柱形的x位置
width = 0.2 #宽度
fig = plt.figure()
ax= fig.add_subplot(111)
ax.bar(ind - width / 2 ,men_means, width, label='男生平均成绩')
ax.bar(ind +0.2 , women_means, width,label='女生平均成绩')
ax.set_title('高二各班男生 、女生英语平均成绩')
ax.set_ylabel('分数')
ax.set_xticks(ind)
ax.set_xticklabels(['高二1班','高二2班','高二3班','高二4班','高二5班','高二6班'])
#添加参考线
plt.axhline(88.5,ls='--',linewidth=1.0,label='全体平均成绩')
ax.legend(loc="lower right")
plt.show()
(8)添加无指向型注释文本
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
x= np.arange(1,8)
y = np.array([1077,16780,24440,30920,37670,48200,57270])
bar_rects = plt.bar(x,y ,tick_label =["FY2013","FY2014","FY2015","FY2016","FY2017","FY20183","FY2019"], width=0.5)
#添加无指向型注释文本
def autolabel(rects):
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width()/ 2 , height + 300 , s='{}'.format(height) , ha='center',va='bottom')
autolabel(bar_rects)
plt.ylabel('GMV(亿元)')
plt.title("2020080603043")
plt.show()
(9)实例5
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(-np.pi,np.pi,256,endpoint=True)
x= np.arange(1,8)
y = np.array([1077,16780,24440,30920,37670,48200,57270])
bar_rects = plt.bar(x,y ,tick_label =["FY2013","FY2014","FY2015","FY2016","FY2017","FY20183","FY2019"], width=0.5)
#添加无指向型注释文本
def autolabel(rects):
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width()/ 2 , height + 300 , s='{}'.format(height) , ha='center',va='bottom')
autolabel(bar_rects)
plt.ylabel('GMV(亿元)')
plt.title("2020080603043")
plt.show()
2.运行结果截图