多语言展示
当前在线:1158今日阅读:112今日分享:19

Python绘制三维图

Python的绘图功能强大,与matlab有得一比,当然不是专业的绘图软件,所以稍微逊色一点。对于一般绘图matlibplot足够了。mpl_toolkits.mplot3d可以用于3D图的绘制。
工具/原料

py3.4

方法/步骤
2

绘制热图:import numpy as npfrom matplotlib import pyplot as pltfrom matplotlib import cm from matplotlib import axesdef draw_heatmap(data,xlabels,ylabels):    #cmap = cm.get_cmap('rainbow',1000)    cmap=cm.gray    figure=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]    for i in data:        for j in i:            if j>vmax:                vmax=j            if j

3

绘制曲面图from matplotlib import pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dfigure = 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

绘制曲线图from mpl_toolkits.mplot3d import Axes3Dimport numpy as npimport matplotlib.pyplot as plt#生成画布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()

注意事项

使用绘图工具时必须明确已经安装相应的模块

推荐信息