Python的绘图功能强大,与matlab有得一比,当然不是专业的绘图软件,所以稍微逊色一点。对于一般绘图matlibplot足够了。mpl_toolkits.mplot3d可以用于3D图的绘制。
工具/原料
py3.4
方法/步骤
1、绘制3D柱状图,其数据格式为,二维数组或三维数组。酆璁冻嘌fromnumpyimport*file=open(&垆杪屑丝#39;C:\Users\jyjh\Desktop\count.txt','r')arr=[]foriinfile.readlines():temp=[]forjini.strip().split('\t'):temp.append(float(j))arr.append(temp)importrandomimportnumpyasnpimportmatplotlibasmplimportmatplotlib.pyplotaspltfrommpl_toolkits.mplot3dimportAxes3Dmpl.rcParams['font.size']=10fig=plt.figure()ax=fig.add_subplot(111,projection='3d')xs=range(len(arr))ys=range(len(arr[0]))forzinrange(len(arr)):xs=range(len(arr))ys=arr[z]color=plt.cm.Set2(random.choice(range(plt.cm.Set2.N)))ax.bar(xs,ys,zs=z,zdir='y',color=color,alpha=0.5)ax.xaxis.set_major_locator(mpl.ticker.FixedLocator(xs))ax.yaxis.set_major_locator(mpl.ticker.FixedLocator(ys))ax.set_xlabel('x')ax.set_ylabel('y')ax.set_zlabel('copies')plt.show()通过设置xs,ys,z可以设定绘制不同维度的数据。
2、绘制热图:importnumpyasnpfr艺皱麾酪ommatplotlibimportpyplotaspltfrom罪焐芡拂matplotlibimportcmfrommatplotlibimportaxesdefdraw_heatmap(data,xlabels,ylabels):#cmap=cm.get_cmap('rainbow',1000)cmap=cm.grayfigure=plt.figure(facecolor='w')ax=figure.add_subplot(2,1,1,position=[1,1,1,1])ax.set_yticks(range(len(ylabels)))ax.set_yticklabels(ylabels)ax.set_xticks(range(len(xlabels)))ax.set_xticklabels(xlabels)vmax=data[0][0]vmin=data[0][0]foriindata:forjini:ifj>vmax:vmax=jifj<vmin:vmin=jmap=ax.imshow(data,interpolation='nearest',cmap=cmap,aspect='auto',vmin=vmin,vmax=vmax)cb=plt.colorbar(mappable=map,cax=None,ax=None,shrink=0.8)plt.show()xl=range(16)yl=range(16)draw_heatmap(arr,xl,yl)
3、绘制曲面图frommatplotlibimportpyplotaspltimportnumpyasnpfrommpl_toolkits.mplot3dimportAxes3Dfigure=plt.figure()ax=Axes3D(figure)X=np.arange(-10,10,0.25)Y=np.arange(-10,10,0.25)#网格化数据X,Y=np.meshgrid(X,Y)R=np.sqrt(X**2+Y**2)Z=np.cos(R)ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='rainbow')plt.show()
4、绘制曲线图frommpl_toolkits.mplot3dimportAxes3Dimportnumpyasnpimportmatplotlib.pyplotasplt#生成画布figure=plt.figure()ax=figure.add_subplot(111,projection='3d')#生成向量z=np.linspace(0,6,1000)r=1x=r*np.sin(np.pi*2*z)y=r*np.cos(np.pi*2*z)ax.plot(x,y,z)plt.show()