ホームページ  >  記事  >  バックエンド開発  >  Python と Matplotlib を使用して 3 次元折れ線グラフを作成する方法

Python と Matplotlib を使用して 3 次元折れ線グラフを作成する方法

WBOY
WBOY転載
2023-04-22 13:19:081906ブラウズ

1.0 はじめに

三次元画像技術は、世界で最も先進的なコンピュータ表示技術の 1 つであり、通常のコンピュータにプラグインをインストールするだけで、Web ブラウザ上で三次元製品を表示できます。本物そっくりであるだけでなく、製品の組み合わせプロセスを動的に表示することができ、特にリモートブラウジングに適しています。

3 次元の画像は視覚的にはっきりと色鮮やかで、視覚的なインパクトが強いため、視聴者はそのシーンに長時間留まり、深い印象を残すことができます。 3次元の絵は人々にリアルで生きているような感覚を与え、キャラクターはすぐに見られるようになり、没入感があり、芸術的鑑賞価値が高くなります。

2.0 3 次元描画方法とタイプ

まず、Matplotlib ライブラリをインストールする必要があります。pip を使用できます。

pip install matplotlib

matplotlib ツール パッケージが既にインストールされていると仮定します。インストールされています。

matplotlib.figure.Figure を使用してプロット フレームを作成します:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Python と Matplotlib を使用して 3 次元折れ線グラフを作成する方法

1. 折れ線プロット

基本的な使用法: ax.plot (x,y,z,label=' ')

コードは次のとおりです:

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

効果は次のとおりです:

Python と Matplotlib を使用して 3 次元折れ線グラフを作成する方法

##2 、散布図

基本構文:

ax.scatter(xs, ys, zs, s=20, c=None, Depthshade=True, *args , * kwargs)

コードは大まかに次のとおりです:

  • xs,ys,zs: 入力データ;

  • s : 散布点のサイズ

  • c: 色、c = 'r’ の場合は赤;

  • Depthshase: 透明度、True は透明です、デフォルトは True、False は不透明です

  • *args などは、maker = ‘o’ などの展開変数であり、散布結果は ’o&lsquo の形状になります;

サンプル コード:

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 と Matplotlib を使用して 3 次元折れ線グラフを作成する方法##3. ワイヤーフレーム プロット

基本使用法: ax.plot_wireframe(X, Y, Z, *args, **kwargs)

XX,Y,Z: 入力データ
    # rstride: 行ステップ長
  • ##cstride: 列ステップ長
  • rcount: 行番号の上限

  • ##ccount: 列数の上限

  • サンプルコード:

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

  • 4. 三曲面プロット

  • 基本的な使用法: ax.plot_trisurf(*args, **kwargs)

ax.plot_trisurf(*args, **kwargs)

X,Y,Z:dataPython と Matplotlib を使用して 3 次元折れ線グラフを作成する方法

他のパラメータは surface-plot と同様です

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
 
 
n_radii = 8
n_angles = 36
 
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
 
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
 
 
# points in the (x, y) plane.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
 
 
z = np.sin(-x*y)
 
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
 
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)
 
plt.show()

レンダリングの実行:

#5. ランダム散布図

散布図を使用すると、ランダムな散布図。

関数定義:

Python と Matplotlib を使用して 3 次元折れ線グラフを作成する方法#関数定義

matplotlib.pyplot.scatter(x, y,

s=None, #散布サイズ配列スカラー

c =None, #カラー シーケンス配列, sequence

marker=None, #Point style

cmap=None, #colormap color style

Norm=None, #Normalization 正規化されたカラー キャンプ

vmin=None, vmax =なし、#上記の正規化範囲に対応
alpha=なし、#transparency

linewidths=なし、#linewidth
verts=なし、
#edgecolors =なし、#エッジカラー
データ=なし、
**kwargs
)


サンプルコード:

import numpy as np
import matplotlib.pyplot as plt
#定义坐标轴
fig4 = plt.figure()
ax4 = plt.axes(projection='3d')
 
#生成三维数据
xx = np.random.random(20)*10-5   #取100个随机数,范围在5~5之间
yy = np.random.random(20)*10-5
X, Y = np.meshgrid(xx, yy)
Z = np.sin(np.sqrt(X**2+Y**2))
 
#作图
ax4.scatter(X,Y,Z,alpha=0.3,c=np.random.random(400),s=np.random.randint(10,20,size=(20, 20)))     #生成散点.利用c控制颜色序列,s控制大小
 
plt.show()

効果:



以上がPython と Matplotlib を使用して 3 次元折れ線グラフを作成する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。