反轉軸#

此範例示範兩種反轉軸方向的方法

  • 如果您無論如何都想要設定明確的軸限制,例如透過 set_xlim,您可以交換限制值:set_xlim(4, 0) 而不是 set_xlim(0, 4)

  • 如果您只想在不修改限制的情況下反轉軸,亦即保留現有的限制或現有的自動縮放行為,請使用 Axis.set_inverted

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.01, 4.0, 0.01)
y = np.exp(-x)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6.4,  4), layout="constrained")
fig.suptitle('Inverted axis with ...')

ax1.plot(x, y)
ax1.set_xlim(4, 0)   # inverted fixed limits
ax1.set_title('fixed limits: set_xlim(4, 0)')
ax1.set_xlabel('decreasing x ⟶')
ax1.grid(True)

ax2.plot(x, y)
ax2.xaxis.set_inverted(True)  # inverted axis with autoscaling
ax2.set_title('autoscaling: set_inverted(True)')
ax2.set_xlabel('decreasing x ⟶')
ax2.grid(True)

plt.show()
Inverted axis with ..., fixed limits: set_xlim(4, 0), autoscaling: set_inverted(True)

標籤:元件:軸 繪圖類型:線 等級:初學者

腳本的總執行時間: (0 分鐘 1.530 秒)

由 Sphinx-Gallery 產生圖庫