单元4:Matplotlib库入门
matplotlib.pyplot是绘制各类可视化图形的命令子库,相当于快捷方式
import matplotlib.pyplot as plt
# -*- coding: utf-8 -*- """ Created on Fri Aug 2 10:03:57 2019 @author: ASUS """ import matplotlib.pyplot as plt plt.plot([0,2,4,6,8],[3,1,4,5,2])#x,y轴坐标 plt.ylabel("grade") plt.xlabel("haha") plt.axis([0,8,0,6])#x,y轴范围 plt.savefig('test1',dpi=600) plt.show
plt.subplot(nrows,nclos,plot_number)
# -*- coding: utf-8 -*- """ Created on Fri Aug 2 10:03:57 2019 @author: ASUS """ import numpy as np import matplotlib.pyplot as plt 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--') plt.show()
plt.plot(x,y,format_string,**kwargs)
format_string:控制曲线的格式字符串,可选
颜色字符:
风格字符:
标记字符:
color:控制颜色,color='green'
linestyle:线条颜色,linestyle='dashed'
marker:标记风格,marker='o'
markerfacecolor:标记颜色,markerfacecolor='blue'
markersize:标记尺寸
**kwargs:第二组或更多
中文显示:
第一种方法:pyplot并不默认支持中文显示,需要rcParams修改字体实现
# -*- coding: utf-8 -*- """ Created on Fri Aug 2 10:03:57 2019 @author: ASUS """ import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams['font.family']='SimHei' #'font.family'用于显示字体的名字 #'font.style'字体风格,正常'normal'或斜体'italic' #'font-size'字体大小,整数字号或者'large'、'x-small' a = np.arange(10) plt.plot(a,a*1.5,'go-',a,a*2.5,'rx',a,a*3.5,'*',a,a*4.5,'b-.') plt.ylabel("纵轴(值)") plt.show()
第二种办法:
fontproperties
# -*- coding: utf-8 -*- """ Created on Fri Aug 2 10:03:57 2019 @author: ASUS """ import numpy as np import matplotlib.pyplot as plt #import matplotlib a = np.arange(0.0,5.0,0.02) plt.xlabel("横轴:时间",fontproperties='SimHei',fontsize=20) plt.ylabel("纵轴:振幅",fontproperties='SimHei',fontsize=10) plt.plot(a,np.cos(2*np.pi*a),'r--') plt.show()
#会遇到负号显示乱码
#添加plt.rcParams['axes.unicode_minus'] = False 即可
文本显示函数:
plt.tiltle() 对图形整体增加文本标签
plt.text() 在任意位置增加文本
plt.annotate(s,xy=arrow_crd,xytext=text_crd,arrowprops=dict) 在图形中增加带箭头的注解
import numpy as np import matplotlib.pyplot as plt #import matplotlib a = np.arange(0.0,5.0,0.02) plt.plot(a,np.cos(2*np.pi*a),'r--') plt.rcParams['axes.unicode_minus'] = False plt.xlabel("横轴:时间",fontproperties='SimHei',fontsize=15,color='green') plt.ylabel("纵轴:振幅",fontproperties='SimHei',fontsize=15) plt.title(r'正弦波实例$y=cos(2\pi x)$',fontproperties='SimHei',fontsize=25) plt.text(2,1,r'$\nu=100$',fontsize=15)#plt.annotate(r'$\mu=100$',xy=(2,1),xytext=(3,1.5),arrowprops=dict(facecolor='black',shrink=0.1,width=2)) plt.axis([-1,6,-2,2]) plt.grid(True) plt.savefig('test2') plt.show()
pyplot子绘图区域
plt.subplot2grid(GridSpec,CurSpec,colspan=1,rowspan=1)
或
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec gs = gridspec.GridSpec(3,3) ax1 = plt.subplot(gs[0,:]) ax2 = plt.subplot(gs[1,:-1]) ax3 = plt.subplot(gs[1:,-1]) ax2 = plt.subplot(gs[2,0]) ax2 = plt.subplot(gs[2,1]) plt.savefig('test3')
单元5:Matplotlib基础绘图函数示例
pyplot饼图的绘制
# -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ import matplotlib.pyplot as plt labels = 'Frogs','Hogs','Dogs','Logs' sizes = [15,30,45,10] explode = (0,0.1,0,0) plt.pie(sizes,explode=explode,labels = labels,autopct='%1.1f%%',shadow=False,startangle=90) plt.axis('equal') plt.show()
pyplot直方图的绘制
# -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ import numpy as np import matplotlib.pyplot as plt np.random.seed(0) mu,sigma = 100,20 a = np.random.normal(mu,sigma,size=100) plt.hist(a,40,normed=0,histtype='stepfilled',facecolor='b',alpha=0.75) #直方图的个数 #normed = 1概率,normed = 0,个数 plt.title('Histogram') plt.show()
pyplot极坐标图
import numpy as np import matplotlib.pyplot as plt N = 20 theta = np.linspace(0.0,2*np.pi,N,endpoint=False) radii = 10*np.random.rand(N) width = np.pi / 4*np.random.rand(N) ax = plt.subplot(111,projection='polar') bars = ax.bar(theta,radii,width=width,bottom=0.0) #left height width for r,bar in zip(radii,bars): bar.set_facecolor(plt.cm.viridis(r/10.)) bar.set_alpha(0.5) plt.savefig('test') plt.show()
pyplot散点图绘制
import numpy as np import matplotlib.pyplot as plt fig,ax = plt.subplots() ax.plot(10*np.random.randn(100),10*np.random.randn(100),'o') ax.set_title('Simple Scatter') plt.show()