在 Matplotlib 中設定 3D 繪圖的縱橫比


要在 matplotlib 中設定 3D 繪圖的縱橫比,我們可以採取以下步驟:

  • 使用 figure() 方法,建立一個新的圖形或啟用一個現有的圖形。
  • 獲取當前的座標軸,必要時建立一個,並指定 projection='3d'。
  • 使用 numpy 建立資料點 R、Y 和 z。
  • 使用 R、Y 和 z 建立一個曲面圖。
  • 使用 set_aspect('auto') 設定縱橫比。
  • 使用 savefig() 方法儲存該圖形。

示例

from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.gca(projection='3d')
R, Y = np.meshgrid(np.arange(0, 100, 1), np.arange(0, 60, 1))
z = 0.1 * np.abs(np.sin(R / 40) * np.sin(Y / 6))
ax.plot_surface(R, Y, z, cmap=cm.rainbow, linewidth=0)
ax.set_aspect('auto')
ax.azim = -160
ax.elev = 30
fig.savefig('data.png')
plt.show()

輸出

更新於: 2021 年 5 月 15 日

3K+ 瀏覽量

開啟你的 職業生涯

完成課程以獲得認證

入門
廣告