import matplotlib.pyplot as plt #import pyplot,using alias
import matplotlib.gridspec as gridspec #grid
import matplotlib #for chinese font
import numpy as np
plt.plot([33,444,5555,66666]) #plot只有一个输入时,参数被当作Y轴,X轴以索引自动生成
plt.ylabel('grade') #y轴标签
plt.savefig('test',dpi=600) #默认为PNG文件,dpi为图片质量
plt.show()
plt.plot([0,2,4,6,8],[3,1,4,5,2]) #plot有两个参数,按照X轴和Y轴顺序绘制数据
plt.ylabel('grade')
plt.axis([-1,10,0,6]) #列表里为四个数字,前两个为X轴的范围,后两个为Y轴的范围
plt.show()
plt.subplot(3,2,4) #也可以是subplot(324)
<matplotlib.axes._subplots.AxesSubplot at 0x7b62f30>
def f(t):
return np.exp(-t)*np.cos(2*np.pi*t)
a = np.arange(0.0,5.0,0.02)
plt.subplot(211)
plt.plot(a,f(a))
plt.subplot(2,1,2)
plt.plot(a,np.cos(2*np.pi*a),'r--') #r表示红色,--表示虚线
plt.show()
a=np.arange(10)
matplotlib.rcParams['font.family'] = 'SimHei' #Chinese characteric,including SimHei,Kaiti,LiSu,FangSong etc
matplotlib.rcParams['font.style']='normal' #include normal,italic
matplotlib.rcParams['font.size']='10' #a integer express font size
plt.ylabel('李子文')
plt.plot(a,a*1.5,'rp',a,a*2.5,'g'' ',a,a*5.5,'#eeeeee') #多条曲线,颜色,风格,标记组合使用
plt.show()
s = np.arange(0.0,5.0,0.02)
matplotlib.rcParams['font.family']='SimHei'
plt.xlabel('横轴:时间',fontproperties='SimHei',fontsize=20,color='green') #xlabel 设置fontproperties='SimHei',fontsize
plt.ylabel('纵轴:振幅',fontproperties='SimHei',fontsize=20)
plt.title('余弦',fontproperties='Kaiti',fontsize=20)
plt.text(0.5,0.5,'hello',fontsize=20)
plt.plot(s,np.cos(2*np.pi*s),'r--')
plt.annotate('asdfasdf',xy=(1,2),xytext=(0.5,0.5),arrowprops=dict(facecolor='black',shrink=0.1,width=2))
plt.axis([-1,2,-1,2])
plt.grid(True) # 表格grid
plt.show()
gs = gridspec.GridSpec(3,3) #grid the area
ax1 = plt.subplot(gs[0,:])
ax2 = plt.subplot(gs[1,:-1])
ax3 = plt.subplot(gs[1,-1])
ax4 = plt.subplot(gs[2,0])
ax5 = plt.subplot(gs[2,1])
ax5.plot([1,2,3,4],[2,3,4,5])
plt.show()