ホームページ  >  記事  >  バックエンド開発  >  Pythonは3Dグラフィックスを描画します

Pythonは3Dグラフィックスを描画します

不言
不言オリジナル
2018-05-03 11:44:3910303ブラウズ

この記事では、主に 3D グラフィックスを描画するための Python を紹介します。これは、必要な友人に参考にしていただけるようになりました。 3D グラフィックスは、データ分析、データ モデリング、グラフィックス、画像の分野で使用されます。以下では、Python を使用して 3D 散布点、3D 面、3D 輪郭、3D 直線 (曲線)、3D の描画を含む 3D グラフィックスを描画する方法を紹介します。文章。

準備作業:

Python で 3D グラフィックスを描画するには、引き続きよく使用される描画モジュール matplotlib を使用しますが、mpl_toolkits ツールキットをインストールする必要があります。インストール方法は次のとおりです: Python インストール ディレクトリの Scripts フォルダーに移動します。 Windows のコマンド ラインを使用して次を実行します。 pip --upgrade matplotlib をインストールするだけで、このコマンドを Linux 環境で直接実行できます。

このモジュールをインストールした後、mpl_tookits の下で mplot3d クラスを呼び出して 3D グラフィックスを描画できます。

以下は一例です。

1 スパイラル 3D 曲線、結果は次のとおりです:

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

描画結果は次のとおりです:

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

描画結果は以下の通りです:

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

描画結果は以下の通りです:

6. 描く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()

描画 結果は次のとおりです:

7. 3D 散布図を描画します

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

結果は次のとおりです:

8 . 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(


描画結果は次のとおりです:

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

の描画結果は次のとおりです。

関連推奨事項:

よく使われるチャートを描くにはPythonを使う

Pythonでグラフィックを描く 例の詳しい説明


以上がPythonは3Dグラフィックスを描画しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。