python matplotlib繪製三維圖的示例
阿新 • • 發佈:2020-09-27
作者: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 plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111,projection='3d')
3D曲線圖
import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt mpl.rcParams['legend.fontsize'] = 10 fig = 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 + 1 x = 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 Axes3D plt.gca(projection='3d') plt.plot([1,3],[3,4,1],[8,'--') plt.xlabel('X') plt.ylabel('Y') #plt.zlabel('Z') #無法使用
3D散點圖
import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt def randrange(n,vmin,vmax): return (vmax-vmin)*np.random.rand(n) + vmin fig = plt.figure() ax = fig.add_subplot(111,projection='3d') n = 100 for c,m,zl,zh in [('r','o',-50,-25),('b','^',-30,-5)]: xs = randrange(n,23,32) ys = randrange(n,100) zs = randrange(n,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繪製三維圖的資料請關注我們其它相關文章!