最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python繪制3D圖形

 更新時間:2018年05月03日 11:14:19   作者:PHILOS_THU  
這篇文章主要介紹了Python繪制3D圖形,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值

3D圖形在數(shù)據(jù)分析、數(shù)據(jù)建模、圖形和圖像處理等領(lǐng)域中都有著廣泛的應(yīng)用,下面將給大家介紹一下如何使用python進(jìn)行3D圖形的繪制,包括3D散點、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í)行該命令。

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

下面以實例進(jìn)行說明。

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()

這段代碼用于繪制一個螺旋狀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()

繪制結(jié)果如下:


5、繪制3D網(wǎng)狀線

from mpl_toolkits.mplot3d import axes3d 
import matplotlib.pyplot as plt 
 
 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
 
# Grab some test data. 
X, Y, Z = axes3d.get_test_data(0.05) 
 
# Plot a basic wireframe. 
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) 
 
plt.show()

繪制結(jié)果如下:


6、繪制3D三角面片圖

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 
 
 
n_radii = 8 
n_angles = 36 
 
# Make radii and angles spaces (radius r=0 omitted to eliminate duplication). 
radii = np.linspace(0.125, 1.0, n_radii) 
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False) 
 
# Repeat all angles for each radius. 
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) 
 
# Convert polar (radii, angles) coords to cartesian (x, y) coords. 
# (0, 0) is manually added at this stage, so there will be no duplicate 
# points in the (x, y) plane. 
x = np.append(0, (radii*np.cos(angles)).flatten()) 
y = np.append(0, (radii*np.sin(angles)).flatten()) 
 
# Compute z to make the pringle surface. 
z = np.sin(-x*y) 
 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
 
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True) 
 
plt.show(

繪制結(jié)果如下:


7、繪制3D散點圖

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 
 
 
def randrange(n, vmin, vmax): 
 ''''' 
 Helper function to make an array of random numbers having shape (n, ) 
 with each number distributed Uniform(vmin, vmax). 
 ''' 
 return (vmax - vmin)*np.random.rand(n) + vmin 
 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
 
n = 100 
 
# For each set of style and range settings, plot n random points in the box 
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh]. 
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]: 
 xs = randrange(n, 23, 32) 
 ys = randrange(n, 0, 100) 
 zs = randrange(n, zlow, zhigh) 
 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()

繪制結(jié)果如下:


8、繪制3D文字

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
 
 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
 
# Demo 1: zdir 
zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1)) 
xs = (1, 4, 4, 9, 4, 1) 
ys = (2, 5, 8, 10, 1, 2) 
zs = (10, 3, 8, 9, 1, 8) 
 
for zdir, x, y, z in zip(zdirs, xs, ys, zs): 
 label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir) 
 ax.text(x, y, z, label, zdir) 
 
# Demo 2: color 
ax.text(9, 0, 0, "red", color='red') 
 
# Demo 3: text2D 
# Placement 0, 0 would be the bottom left, 1, 1 would be the top right. 
ax.text2D(0.05, 0.95, "2D Text", transform=ax.transAxes) 
 
# Tweaking display region and labels 
ax.set_xlim(0, 10) 
ax.set_ylim(0, 10) 
ax.set_zlim(0, 10) 
ax.set_xlabel('X axis') 
ax.set_ylabel('Y axis') 
ax.set_zlabel('Z axis') 
 
plt.show(

繪制結(jié)果如下:


9、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') 
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]): 
 xs = np.arange(20) 
 ys = np.random.rand(20) 
 
 # You can provide either a single color or an array. To demonstrate this, 
 # the first bar of each set will be colored cyan. 
 cs = [c] * len(xs) 
 cs[0] = 'c' 
 ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8) 
 
ax.set_xlabel('X') 
ax.set_ylabel('Y') 
ax.set_zlabel('Z') 
 
plt.show() 

繪制結(jié)果如下:

以上所述是小編給大家介紹的python繪制3D圖形,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持

相關(guān)文章

  • Python基于plotly模塊實現(xiàn)的畫圖操作示例

    Python基于plotly模塊實現(xiàn)的畫圖操作示例

    這篇文章主要介紹了Python基于plotly模塊實現(xiàn)的畫圖操作,涉及Python基于plotly模塊的數(shù)值運算與圖形操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-01-01
  • 對python append 與淺拷貝的實例講解

    對python append 與淺拷貝的實例講解

    今天小編就為大家分享一篇對python append 與淺拷貝的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 利用matplotlib+numpy繪制多種繪圖的方法實例

    利用matplotlib+numpy繪制多種繪圖的方法實例

    matplotlib是Python最著名的繪圖庫,本文給大家分享了利用matplotlib+numpy繪制多種繪圖的方法實例,其中包括填充圖、散點圖(scatter plots)、. 條形圖(bar plots)、等高線圖(contour plots)、 點陣圖和3D圖,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能

    Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能

    這篇文章主要介紹了Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能,實現(xiàn)思路很簡單,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05
  • 十行Python3代碼實現(xiàn)去除pdf文件水印

    十行Python3代碼實現(xiàn)去除pdf文件水印

    pfd文檔一般無法直接去除水印,需要先將pfd文檔轉(zhuǎn)換成圖片,在逐一對圖片進(jìn)行水印去除操作,最后在把圖片插入到pdf文檔中,這樣就很繁瑣。本文將用十行Python3代碼輕輕松松實現(xiàn)PDF文件水印去除,快來了解一下吧
    2022-02-02
  • python聊天程序?qū)嵗a分享

    python聊天程序?qū)嵗a分享

    這篇文章主要介紹了用python寫的聊天程序,開兩個線程,即是客戶端,也是服務(wù)器,大家可以參考使用
    2013-11-11
  • python實現(xiàn)自動化辦公郵件合并功能

    python實現(xiàn)自動化辦公郵件合并功能

    這篇文章主要介紹了python實現(xiàn)自動化辦公郵件合并功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • python命令行工具Click快速掌握

    python命令行工具Click快速掌握

    這篇文章主要介紹了python命令行工具Click快速掌握,寫 Python 的經(jīng)常要寫一些命令行工具,雖然標(biāo)準(zhǔn)庫提供有命令行解析工具 Argparse,但是寫起來非常麻煩,我很少會使用它。命令行工具中用起來最爽的就是 Click,,需要的朋友可以參考下
    2019-07-07
  • Python splitlines使用技巧

    Python splitlines使用技巧

    Python中的splitlines用來分割行。當(dāng)傳入的參數(shù)為True時,表示保留換行符 \n。通過下面的例子就很明白了
    2008-09-09
  • Django管理員賬號和密碼忘記的完美解決方法

    Django管理員賬號和密碼忘記的完美解決方法

    這篇文章主要給大家介紹了關(guān)于Django管理員賬號和密碼忘記的完美解決方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧
    2018-12-12

最新評論

台前县| 宜兰市| 东宁县| 蛟河市| 班玛县| 巴林右旗| 宁河县| 静海县| 玛多县| 普定县| 古浪县| 固安县| 姜堰市| 泰宁县| 察哈| 那坡县| 北碚区| 临桂县| 德保县| 嘉荫县| 成安县| 台湾省| 府谷县| 疏附县| 台中县| 通州市| 乐都县| 綦江县| 延吉市| 卢湾区| 公主岭市| 凤翔县| 阳朔县| 任丘市| 宕昌县| 桐梓县| 蓝山县| 广饶县| 淳安县| 西峡县| 如皋市|