注意
转到末尾下载完整示例代码。
倒置坐标轴#
本示例演示了两种倒置坐标轴方向的方法
如果您无论如何都想设置明确的坐标轴限制,例如通过
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()

脚本总运行时间:(0 分钟 1.208 秒)