import numpy as np
import matplotlib.pyplot as plt
import matplotlib
##################################################################################
##绘制基本操作
##设置字体和大小等格式
params={'font.family':'serif',
'font.serif':'Times New Roman',
'font.style':'normal',#or italic 斜体
'font.weight':'normal', #or 'blod'
#'font.size':'medium',#or large,small
}
matplotlib.rcParams.update(params)#载入设置
##plot
x=np.linspace(0,10,30)#等间距(start,end,num.)生成矩阵
l1,=plt.plot(x,np.cos(x),'ro-')#输入数据可为列表或者数组,r:红色,o点画圈,-直线
l2,=plt.plot(x,np.sin(x),'bx--')
plt.ylabel('output')#y坐标
plt.xlabel('input')#x坐标
plt.axis([0,10,-1,1])#设置横纵坐标区间[x_0,x_end,y_0,y_end]
plt.title('cos & sin functions')#标题
plt.legend([l1,l2], ['curve 1#','curve 2#'],loc='upper right')#设置图例,upper right:右上角
## way1 单独文字
plt.text(1.8,np.cos(1.8),r'$y=cos(x)$',fontsize=12)#(x,y,字符串,字体大小),xy为字符串位置
plt.text(2,np.sin(2),r'$y=sin(x)$',fontsize=12)
plt.savefig('a1.png',dpi=600)
plt.show()
## way2 箭头指示
plt.annotate('y=cos(x)',xy=(1.8,np.cos(1.8)),xytext=(2.5,np.cos(1.8)+0.2),arrowprops=dict
(facecolor='black',shrink=0.05,width=0.5,headwidth=5,headlength=5))
#(‘显示的字符串’。xy:标记的点位置,xytext:字符串位置,arrowprops:设置箭头格式)
plt.annotate('y=sin(x)',xy=(2,np.sin(2)),xytext=(3,np.sin(2)-0.2),arrowprops=dict
(facecolor='black',shrink=0.05,width=0.5,headwidth=5,headlength=5))
plt.savefig('a2.png',dpi=600)#保存图片
plt.show()#显示图片
子绘图分区
##简单分区
plt.subplot(2,1,1)
plt.plot(np.arange(10),2*np.arange(10)+2,'y--')
plt.subplot(2,1,2)
plt.plot()
plt.plot(np.arange(10),3*np.arange(10)+2,'g--')
plt.savefig('a3.png',dpi=600)#保存图片
plt.show()
##复杂分区
gs=gridspec.GridSpec(4,4)
ax1=plt.subplot(gs[0,:])#第一行区域
ax1.plot(x,np.cos(x),'ro-')
ax2=plt.subplot(gs[1,:-1])#
ax2.plot(x,np.sin(x),'bv--')
ax3=plt.subplot(gs[1:,-1])#
ax3.plot(x,np.cos(x),'c^-.')
ax4=plt.subplot(gs[2:,:-2])#
ax4.plot(x,np.sin(x),'mh:')
ax5=plt.subplot(gs[2,2])#
ax5.plot(x,np.cos(x),'yD--')
ax6=plt.subplot(gs[3,2])#
ax6.plot(x,np.sin(x),'k*')
plt.savefig('a4.png',dpi=600)#保存图片
plt.show()
基础绘图函数
饼图
labels='Teachers', 'Students', 'Lawers','Artists'
sizes=[15,30,45,10]
explode=[0,0,0.1,0]#每个pie的半径相对于初始半径的偏移量;# only "explode" the 3nd slice (i.e. 'Lawers')
fig1, ax1 = plt.subplots()
wedges, texts, autotexts = ax1.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',
shadow=False,startangle=90,textprops=dict(color="w"))
ax1.legend(wedges, labels,
title="Labels",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=8, weight="bold")
ax1.set_title("A pie for groups")
ax1.axis('equal') # 保证画成一个圆
plt.show()
直方图
np.random.seed(0)
mu,sigma=100,20
a=np.random.normal(mu,sigma,size=100)
plt.hist(a,20,normed=1,histtype='bar',orientation='horizontal',facecolor='b',alpha=0.75)#'horizontal':水平方向,'vertical':垂直方向
plt.title('Histogram')
plt.savefig('a6.png',dpi=600)#保存图片
plt.show
箱线图
a=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
labels=['A','B','C']
plt.boxplot(a,notch=False,labels=labels)#notch:False 无缺口
plt.title('Multiple Samples')
plt.savefig('a7.png',dpi=600)#保存图片
plt.show()
更多查看官方文档
网址为:https://matplotlib.org/3.2.2/index.html。官方网址对每个函数进行了详细的介绍,包含参数部分,同时提供的samples,非常方便使用。