国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python繪制3D圖形

2020-02-22 23:58:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

3D圖形在數(shù)據(jù)分析、數(shù)據(jù)建模、圖形和圖像處理等領(lǐng)域中都有著廣泛的應(yīng)用,下面將給大家介紹一下如何使用python進(jìn)行3D圖形的繪制,包括3D散點(diǎn)、3D表面、3D輪廓、3D直線(曲線)以及3D文字等的繪制。

準(zhǔn)備工作:

python中繪制3D圖形,依舊使用常用的繪圖模塊matplotlib,但需要安裝mpl_toolkits工具包,安裝方法如下:windows命令行進(jìn)入到python安裝目錄下的Scripts文件夾下,執(zhí)行: pip install --upgrade matplotlib即可;linux環(huán)境下直接執(zhí)行該命令。

安裝好這個(gè)模塊后,即可調(diào)用mpl_tookits下的mplot3d類進(jìn)行3D圖形的繪制。

下面以實(shí)例進(jìn)行說(shuō)明。

1、3D表面形狀的繪制

from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np  fig = plt.figure() ax = fig.add_subplot(111, projection='3d')  # Make data u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) x = 10 * np.outer(np.cos(u), np.sin(v)) y = 10 * np.outer(np.sin(u), np.sin(v)) z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))  # Plot the surface ax.plot_surface(x, y, z, color='b')  plt.show()

球表面,結(jié)果如下:


2、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()  plt.show()

這段代碼用于繪制一個(gè)螺旋狀3D曲線,結(jié)果如下:


3、繪制3D輪廓

from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt from matplotlib import cm  fig = plt.figure() ax = fig.gca(projection='3d') X, Y, Z = axes3d.get_test_data(0.05) cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm) cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm) cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)  ax.set_xlabel('X') ax.set_xlim(-40, 40) ax.set_ylabel('Y') ax.set_ylim(-40, 40) ax.set_zlabel('Z') ax.set_zlim(-100, 100)  plt.show()

繪制結(jié)果如下:


4、繪制3D直方圖

from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np  fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x, y = np.random.rand(2, 100) * 4 hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]])  # Construct arrays for the anchor positions of the 16 bars. # Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos, # ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid # with indexing='ij'. xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25) xpos = xpos.flatten('F') ypos = ypos.flatten('F') zpos = np.zeros_like(xpos)  # Construct arrays with the dimensions for the 16 bars. dx = 0.5 * np.ones_like(zpos) dy = dx.copy() dz = hist.flatten()  ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')  plt.show()            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长子县| 赤城县| 汾阳市| 灵璧县| 荥经县| 蓬溪县| 青川县| 宁陕县| 宿州市| 平乐县| 城口县| 合山市| 泌阳县| 中牟县| 德安县| 蓝山县| 嫩江县| 同仁县| 龙江县| 萨迦县| 潜江市| 二手房| 丹阳市| 江华| 龙口市| 新津县| 武鸣县| 古交市| 南通市| 县级市| 且末县| 翼城县| 呼和浩特市| 广灵县| 新宁县| 儋州市| 金沙县| 望都县| 铜鼓县| 罗源县| 淮安市|