添加图例和标题
import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-2*np.pi,2*np.pi,200)y = np.sin(x)y1 = np.cos(x)plt.plot(x,y,label=r'$sin(x)$') #TeX表达式plt.plot(x,y1,label=r'$cos(x)$')plt.legend(loc='lower left')plt.title('Cos & Sin plot')plt.show()
![0d0a82f87cf7f67ce54bd857de0e896f.png](https://i-blog.csdnimg.cn/blog_migrate/45353c518fd8c19c6f287703e52cb55c.jpeg)
图例样式
x = np.arange(0,10,.1)y = np.power(x,0.5)y1 = np.power(x,1/3)y2 = np.power(x,1/4)plt.plot(x,y,ls='-',lw=2,label='$x^{1/2}$')plt.plot(x,y1,ls='-',lw=2,c='r',label='$x^{1/3}$')plt.plot(x,y2,ls='-',lw=2,c='y',label=r'$x^{1/4}$')plt.legend(loc='upper left',bbox_to_anchor=(.05,.95),ncol=3,,shadow=True,fancybox=True)# upper rigth 1# lower right 4# lower center 8# upper left 2# center left 6# upper center 9# lower left 3# center right 7# center 10# bbox_to_anchor(x轴位置百分比,y轴位置百分比,x轴框长度,y轴框高度)plt.show()
![68d0dc6dccb2da0a21ec60d3e361b6ff.png](https://i-blog.csdnimg.cn/blog_migrate/3529efc80ee53b0c60a9c4af1e0ed09f.jpeg)
标题样式
x = np.linspace(-2,2,1000)y = np.exp(x)plt.plot(x,y,ls='-',lw=2,color='g')plt.title('center')plt.title('Left title',loc='left', fontdict={'size':'xx-large', 'color':'r', 'family':'Times New Roman'})plt.title('right title',loc='right', size=20, style='oblique', color='c')plt.show()
![bb427bc0021e7c27e7d7262d49b5957d.png](https://i-blog.csdnimg.cn/blog_migrate/6ed8ec4ff8c478415f70afffea9f8519.jpeg)
饼图图例
labels = [chr(i) for i in range(65,70)]weight = np.random.randint(10,50,5)wedges,texts,autotexts = plt.pie(weight,autopct='%3.2f%%', textprops=dict(color='w'))plt.legend(wedges, # 饼图实例列表 labels, # 文本标签列表 fontsize=12, , loc='center left', bbox_to_anchor=(0.9,0,.2,1))plt.setp(autotexts,size=15,weight='bold')plt.setp(texts,size=12)plt.title('content of samples')plt.show()
![9b7e9b40ffb9f95dedbd106937df02a5.png](https://i-blog.csdnimg.cn/blog_migrate/415e49deeee89580503a9d8d94835cca.jpeg)
刻度范围和刻度标签
x = np.linspace(-2*np.pi,2*np.pi,200)y = np.sin(x)plt.subplot(211) # 2行1列的图形1plt.plot(x,y)plt.subplot(212) #2行1列的图形2plt.xlim(-2*np.pi,2*np.pi)plt.xticks([-2*np.pi,-3*np.pi/2,-1*np.pi,-1*np.pi/2,0,np.pi/2,np.pi,3*np.pi/2,2*np.pi], [r'$-2pi$',r'$-3pi/2$',r'$-pi$',r'$-pi/2$',r'$0$',r'$pi/2$',r'$pi$',r'$3pi/2$',r'$2pi$'])# xticks()修改x轴刻度标签plt.plot(x,y)plt.show()
![ba65134f3a9f88d0179ed03898ca7135.png](https://i-blog.csdnimg.cn/blog_migrate/fe039bbd566358384778aab2f8028212.jpeg)
坐标轴刻度了逆序
xvalues = np.arange(1,11,0.2)yvalues = np.power(xvalues,1/3)+0.9plt.plot(xvalues,yvalues, linestyle='-', linewidth=2, color='r')plt.xlim(10,1)plt.xlabel('axis X')plt.ylabel('axis Y')plt.grid(ls=':',lw=1,color='gray',alpha=.5)plt.show()
![8bdd8e5089076c87cc9f39fc5691f31b.png](https://i-blog.csdnimg.cn/blog_migrate/2246cb9d24191d27e8cd49cbd951c3d5.jpeg)
给图片添加表格
labels = [chr(i) for i in range(65,70)]values = [abs(round(i)) for i in np.random.randint(0,25,4)]values.append(100-sum(values))plt.pie(values, explode=np.repeat(0.1,5), labels=labels, autopct='%1.1f%%', startangle=45, shadow=True)plt.title('Pie Chart')tblabels = labelsrlabels=['Chart Table']tbvalues = [list(np.random.randint(0,500,5))]plt.table(cellText=tbvalues, cellLoc='center', colWidths=[0.1]*5, colLabels=tblabels, rowLabels=rlabels, rowLoc='center', loc='bottom')# cellText 表格数值# colWidths 表格列宽# colLables 表格的列名# colColours 表格列名颜色# rowLabels 表格行名# rowLoc 表格每行行名对齐位置,左,中,右# loc 表格在画布中的位置plt.show()
![0b57fba9e13edc498f22eeec40f55207.png](https://i-blog.csdnimg.cn/blog_migrate/2b2e8757b52fda859e103003b11a5d74.jpeg)