python matplotlib繪制三維圖的示例
作者:catmelo 本文版權歸作者所有
鏈接:https://www.cnblogs.com/catmelo/p/4162101.html
本文參考官方文檔:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
起步
新建一個matplotlib.figure.Figure對象,然后向其添加一個Axes3D類型的axes對象。其中Axes3D對象的創建,類似其他axes對象,只不過使用projection=’3d’關鍵詞。
import matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = fig.add_subplot(111, projection=’3d’)
3D曲線圖
import matplotlib as mplfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as npimport matplotlib.pyplot as pltmpl.rcParams[’legend.fontsize’] = 10fig = plt.figure()ax = fig.gca(projection=’3d’)theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)z = np.linspace(-2, 2, 100)r = z**2 + 1x = r * np.sin(theta)y = r * np.cos(theta)ax.plot(x, y, z, label=’parametric curve’)ax.legend()ax.set_xlabel(’X Label’)ax.set_ylabel(’Y Label’)ax.set_zlabel(’Z Label’)plt.show()
簡化用法:
from pylab import *from mpl_toolkits.mplot3d import Axes3Dplt.gca(projection=’3d’)plt.plot([1,2,3],[3,4,1],[8,4,1],’--’)plt.xlabel(’X’)plt.ylabel(’Y’)#plt.zlabel(’Z’) #無法使用
3D散點圖
import numpy as npfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltdef randrange(n, vmin, vmax): return (vmax-vmin)*np.random.rand(n) + vminfig = plt.figure()ax = fig.add_subplot(111, projection=’3d’)n = 100for c, m, zl, zh in [(’r’, ’o’, -50, -25), (’b’, ’^’, -30, -5)]: xs = randrange(n, 23, 32) ys = randrange(n, 0, 100) zs = randrange(n, zl, zh) ax.scatter(xs, ys, zs, c=c, marker=m)ax.set_xlabel(’X Label’)ax.set_ylabel(’Y Label’)ax.set_zlabel(’Z Label’)plt.show()
以上就是matplotlib繪制三維圖的示例的詳細內容,更多關于matplotlib繪制三維圖的資料請關注好吧啦網其它相關文章!
相關文章: