本文实例讲述了Python基于matplotlib实现绘制三维图形功能。分享给大家供大家参考,具体如下:
代码一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# coding=utf-8 import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.mplot3d x,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.zyiz.net - 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() |
运行结果:
代码二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# coding=utf-8 import pylab as pl import numpy as np import mpl_toolkits.mplot3d rho, theta = np.mgrid[ 0 : 1 : 40j , 0 : 2 * np.pi: 40j ] z = rho * * 2 x = rho * np.cos(theta) y = rho * np.sin(theta) ax = pl.subplot( 111 , projection = '3d' ) ax.set_title( 'www.zyiz.net - 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程序设计有所帮助。
原文链接:http://blog.csdn.net/chengqiuming/article/details/78601390