本文实例讲述了Python基于matplotlib实现绘制三维图形功能。分享给大家供大家参考,具体如下:
代码一:
# coding=utf-8import numpy as npimport matplotlib.pyplot as pltimport mpl_toolkits.mplot3dx,y = np.mgrid[-2:2:20j,-2:2:20j]#测试数据z=x*np.exp(-x**2-y**2)#三维图形ax = plt.subplot(111, projection='3d')ax.set_title('www.vevb.com - matplotlib Demo');ax.plot_surface(x,y,z,rstride=2, cstride=1, cmap=plt.cm.Blues_r)#设置坐标轴标签ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')plt.show()
运行结果:
代码二:
# coding=utf-8import pylab as plimport numpy as npimport mpl_toolkits.mplot3drho, theta = np.mgrid[0:1:40j,0:2*np.pi:40j]z = rho**2x = rho*np.cos(theta)y = rho*np.sin(theta)ax = pl.subplot(111, projection='3d')ax.set_title('www.vevb.com - matplotlib Demo');#ax.plot_surface(x,y,z)ax.plot_surface(x,y,z,rstride=2, cstride=1)#设置坐标轴标签ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')pl.show()
运行结果:
希望本文所述对大家Python程序设计有所帮助。
新闻热点
疑难解答