如何旋轉簡單的 matplotlib 座標軸?


要旋轉簡單的 matplotlib 座標軸,我們可以採取以下步驟 -

  • 匯入所需的包 -
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
  • 設定圖形大小並調整子圖之間和周圍的填充。
  • 建立一個新圖形或啟用一個現有圖形。
  • 製作座標軸極值的元組。
  • 新增一個可變的二維仿射變換,“t”。原地新增一個旋轉(以度為單位)到該變換中。
  • 從源(曲線)座標新增一個變換到目標(直線)座標。
  • 使用 GridHelperCurveLinear() 例項新增一個浮動座標軸 “h” 到當前圖形。
  • 將 'ax' 作為一個子圖排列的一部分新增到圖形中。
  • 要顯示圖形,請使用 show() 方法。

示例

# import the packages
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes

# set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# plot the figure
fig = plt.figure()

scales = (0, 5, 0, 5)

# Add 2D affine transformation
t = Affine2D().rotate_deg(25)

# Add floating axes
h = floating_axes.GridHelperCurveLinear(t, scales)
ax = floating_axes.FloatingSubplot(fig, 111, grid_helper=h)

fig.add_subplot(ax)

plt.show()

輸出

將產生以下輸出 -

更新時間:2021 年 9 月 20 日

10k+ 瀏覽量

開啟你的職業生涯

完成課程,獲得認證

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