倒置坐标轴#

本示例演示了两种倒置坐标轴方向的方法

  • 如果您无论如何都想设置明确的坐标轴限制,例如通过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.208 秒)

由 Sphinx-Gallery 生成的画廊