python matplotlib繪制三維圖的示例
作者:catmelo 本文版權(quán)歸作者所有
鏈接:https://www.cnblogs.com/catmelo/p/4162101.html
本文參考官方文檔:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
起步
新建一個matplotlib.figure.Figure對象,然后向其添加一個Axes3D類型的axes對象。其中Axes3D對象的創(chuàng)建,類似其他axes對象,只不過使用projection=’3d’關(guān)鍵詞。
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散點(diǎn)圖
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繪制三維圖的示例的詳細(xì)內(nèi)容,更多關(guān)于matplotlib繪制三維圖的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 使用css實(shí)現(xiàn)全兼容tooltip提示框2. CSS代碼檢查工具stylelint的使用方法詳解3. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)4. Vue3使用JSX的方法實(shí)例(筆記自用)5. JavaScript數(shù)據(jù)類型對函數(shù)式編程的影響示例解析6. 詳解CSS偽元素的妙用單標(biāo)簽之美7. Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例8. 利用CSS3新特性創(chuàng)建透明邊框三角9. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程10. 不要在HTML中濫用div
