我的python日记(4)Matplotlib

您所在的位置:网站首页 python绘制3d动图 我的python日记(4)Matplotlib

我的python日记(4)Matplotlib

2023-03-14 00:51| 来源: 网络整理| 查看: 265

Matplotlib绘图包-import matplotlib.pyplot as plt3D图、散点图、动图、三维图、等高线图、多图组合成、图中图、局部放大图

1、基本用法

plt.plot(x,y)plt.show()图片操作,位置、保存

2、figure()使用

plt.figure(num=1,figsize=(8,5))--定义一个figure(),可设置显示多个框图plt.plot(x,y,color='',linewidth=,linestyle=,label=)plt.imshow()--显示图片

颜色速查表

线体常用的:实线:-点线:.点划线:-.虚线:--maker

标记速查表3、坐标轴的设置-axis

#坐标轴的数值范围plt.xlim((-1,1))plt.ylim((-1,1))#xlabelplt.xlabel()plt.ylabel()#坐标轴刻度,替换坐标轴标值new_ticks=np.linspace(-1,2,5)plt.xticks(new_ticks)plt.yticks([替换前],[替换后])#坐标轴的移动gca='get current axis'ax=plt.gca()ax.spines[]--图的四个边框'right''top''bottom''left'ax.spines['right'].set_color('none')ax.spines['bottom'].set_position('data',0)axis.xaixs.set_ticks_position('bottom')axis.yaisx.set_ticks_position('left')

4、图例legend

l1,=plt.plot()--设置label,注意返回值句柄l1plt.legend(handles=[l1],labels=,loc='best'/'upper right')--打开legend图例显示,可设置只显示某些图列

5、标注-Annotation

#特定点加注释plt.scatter(x0,y0,s=50,color='b')plt.plot([x0,x0],[y0,0],'k--',lw=2.5)plot.annotate(r'$2x+1=%s$'%y0,xy=(x0,y0),xycoords='data',xytext=(+30,-30),textcoords='offset points',frontsize=16,arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))#区域注解plt.text(x0,y0,r'$This\is\the\some\text.\mu\sigma_i\ \ alpha_t$',fontdict={'size':16 ,'color':red})#清晰坐标轴

6、不同呈现方式

(1) 散点图plt.scatter(x,y,size=,c=(颜色值-0-1之间),alpha=(调整透明度:0-1之间))

(2) 柱状图-每个柱子上标注数字plt.bar(X,+Y1,facecolor='颜色值',edgecolor='white')plt.bar(X,-Y1)for x,y in zip(X,Y1):    plt.text(x+0.4,y+0.05,'%.2f ' % y , ha='center' ,va='bottom')

(3) 等高线图-一个网格,网格往下投影x,y,zeg..n=256x=np.linspace(-3,3,n)y=np.linspace(-3,3,n)z=(1-X/2+X*85+Y**3)*np.exp(-X**2-Y**2)将x,y绑定为网格的输入值:X,Y=np.meshgrid(x,y)而Z也必须基于网格数据得到,才可以绘图plt.contourf(X,Y,Z,8,alpha=0.75,cmap=plt.cm.hot/cool)--8,表示最终分的区域=8+2C=plt.contour(X,Y,Z,8,c='r',linewidth=0.5)#等高线标注plt.clabel(C,inline=True,fontsize=10)#colorbar显示plt.colorbar(shrink=0.9)#压缩表示为只显示图高的一部分

(4) image图片矩阵-转换为图片显示-不同的显示方式plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower'/'upper')

(5) 3D图像from mpl_toolkits.mplot3d import Axes3Dfig=plt.figure()ax=Axes3D(fig)将x,y绑定为网格的输入值:X,Y=np.meshgrid(x,y)而Z也必须基于网格数据得到,才可以绘图ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))下面投影等高线图ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')

(6) subplot 多合一显示、分格显示行,列,当前所占用窗图*******************plt.subplot(2,2,1)plt.plot()plt.subplot(2,2,2)plt.plot()plt.subplot(2,2,3)plt.plot()plt.subplot(2,2,4)plt.plot()*******************L1=plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1)#指定大小,起始点,横纵的方向跨度:不指定则只占用一个格L1返回的是一个对象,可以通过对象操作*******************gs=gridspec.GridSpec(3,3)ax1=plt.subplot(gs[0:])#用索引的方式执行*******************plt.subplots()

(7) 图中图-大图中画小图fig=plt.figure()ax1=fig.add_axes([left,bottom,width,height])ax1.plot()ax1.set_xlabel()ax1=set_title()

(8) 次坐标轴scoemdary--双坐标图ax1=plt.plot()ax2=ax1.twinx()ax1.plot()ax2.plot()

(9) 动画Animation-展示数据from  matplotlib import animationani=animation.FuncAnimation(fig=fig,func=,frames=100,init_func=,interval=20,blit=True)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3