matplotlib.pyplot及Matplotlib.pylab都是matplotlib中的一个模块。
pyplot 是 matplotlib 一个子模块,主要为底层的面向对象的绘图库提供状态机界面。状态机隐式地自动创建数字和坐标轴以实现所需的绘图。
Pylab将pyplot function(用于绘图)与numpy function(用于数学和处理数组)结合在一个命名空间中,使得该命名空间(或环境)更像MATLAB。 例如,可以像在MATLAB中一样调用sin和cos函数,以及具有pyplot的所有function。
对于非交互式绘图 (即脚本), 通常优选pyplot接口 。 pylab界面便于交互式计算和绘图 ,因为它最大限度地减少了打字。 请注意,如果您将ipython shell与-pylab选项一起使用,即可从pylab导入所有内容并使绘图完全交互,这就是您所获得的。
importmatplotlib.pyplotasplt
frompylabimport*
importnumpyasnp
main_image=plt.figure(figsize=(10,10))
subplots_adjust(hspace=0.3,wspace=0.3)#控制子图间的行间距、列间距
#子图1-单线
x_0=np.linspace(,2*np.pi,20)#自变量X的取值范围
sub_image_1=plt.subplot(2,2,1)
plt.xlabel('X value')
plt.ylabel('Sin value')
plt.grid(True)
sub_image_1.plot(x_0,np.sin(x_0),'r--o',label='Sin(x)')
sub_image_1.legend()#展示图例
sub_image_1.annotate('sin wave',xy=(3,0.25),xytext=(4,0.5),arrowprops=dict(facecolor='black',shrink=0.05))#特定文本注释
sub_image_1.set_title('Sin Waves')
#子图2-多线
x_1=np.linspace(,2*np.pi,20)
sub_image_2=plt.subplot(2,2,2)
plt.xlabel('X value')
plt.ylabel('Cos and Sin value')
plt.grid(True)
sub_image_2.plot(x_1,np.cos(x_1),color='blue',linestyle='--',linewidth=1,marker='o',markerfacecolor='red',markersize='6',label='Cos(x)')
sub_image_2.plot(x_1,np.sin(x_1),color='green',linestyle='-.',linewidth=3,marker='^',markerfacecolor='yellow',markersize='8',label='Sin(x)')
sub_image_2.legend()
sub_image_2.set_title('Cos and Sin Waves')
#子图3-直方图
bins_count=10
mu,sigma=100,20
x_hist=mu+sigma*np.random.randn(1000,1)#randn用于生成符合标准正态分布的包含1000个元素的列序列
sub_image_3=plt.subplot(2,2,3)
plt.xlabel('value')
plt.ylabel('count')
plt.grid(False)
tuple_return=sub_image_3.hist(x_hist,bins=bins_count,facecolor='red',alpha=0.8,edgecolor='black',normed=)#normed=0画频数直方图,normed=1画频率直方图
sub_image_3.set_title('Frequency Histogram')
plt.xlim((floor(x_hist.min()),ceil(x_hist.max())))
bar_width=(x_hist.max()-x_hist.min())/bins_count
plt.xticks(np.arange(floor(x_hist.min()),ceil(x_hist.max()),round(bar_width)))#刻度设置
foriinrange(bins_count):
sub_image_3.text(x_hist.min()+(bar_width*i)+(bar_width/2),tuple_return[][i],str(tuple_return[][i]),horizontalalignment='center',verticalalignment='bottom')
#子图3-分段函数
x_part_1=np.linspace(-10,-1,10)#分段函数的离散取值
x_part_2=np.linspace(,10,11)
sub_image_4=plt.subplot(2,2,4)
plt.xlabel('X value')
plt.ylabel('Y value')
plt.grid(False)
sub_image_4.plot(x_part_1,x_part_1*2+1,'b--o',label='y=2x+1')
sub_image_4.plot(x_part_2,x_part_2**2,'r--o',label='y=x^2')
sub_image_4.legend()
sub_image_4.set_title('PieceWise Function')
#展示
plt.show()
领取专属 10元无门槛券
私享最新 技术干货