在 Matplotlib 的 3D 圖形中為向量新增箭頭


要在 3D matplotlb 的圖形中繪製箭頭頭的向量,我們可以採取以下步驟 -

  • 建立一個二維陣列,其中 x、y、z、u、v和w 是箭頭位置的座標以及箭頭向量方向的元件。

  • 使用 figure() 方法建立一個新圖形或啟用一個現有圖形。

  • 使用 add_subplot() 方法將 '~.axes.Axes' 作為子圖排列的一部分新增到圖形中。

  • 使用 quiver() 方法繪製箭頭 3D 域。

  • 使用 ylim, xlim, zlim, 限制各個軸的範圍。

  • 設定圖形的標題。

  • 要顯示圖形,請使用 show() 方法。

示例

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
soa = np.array([[0, 0, 1, 1, -2, 0], [0, 0, 2, 1, 1, 0], [0, 0, 3, 2, 1, 0], [0, 0, 4, 0.5, 0.7, 0]])
X, Y, Z, U, V, W = zip(*soa)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.quiver(X, Y, Z, U, V, W, color='red')
ax.set_xlim([-1, 0.5])
ax.set_ylim([-1, 1.5])
ax.set_zlim([-1, 8])
ax.set_title("Vectors")
plt.show()

輸出

更新時間: 07-5-2021

2 千次+ 瀏覽

開啟您的事業

完成課程認證

立即開始
廣告
© . All rights reserved.