【问题标题】:matplotlib.pyplot: sharing axes' x-axis after plotting doesn't put them on the same x-axis limits [duplicate]matplotlib.pyplot:绘图后共享轴的 x 轴不会将它们置于相同的 x 轴范围内
【发布时间】:2019-05-05 17:28:35
【问题描述】:

我正在尝试共享 matplotlib 轴 (例如 axsA)它们被 绘制(不仅仅是在它们被 created 之后)。与我的预期相反,在进行连接后,即使我执行 plt.draw() 和 plt.show(),轴也不会使用共享 xlim 重绘:

import matplotlib.pyplot as plt

# Share two subplot axes AFTER plotting
figA, axsA = plt.subplots(1, 2, sharex=False)
axsA[0].plot(range(0, 10), range(10))
axsA[1].plot(range(3, 13), range(10))
axsA[0].get_shared_x_axes().join(axsA[0], axsA[1])
figA.suptitle('Join after plotting: x-axes limits are not the same in the two axes.')
plt.draw()
plt.show()

只有在绘图之前将它们加入到 Grouper 对象中时,我似乎才能实现共享轴:

# Share two subplot axes BEFORE plotting
figB, axsB = plt.subplots(1, 2, sharex=False)
axsB[0].get_shared_x_axes().join(axsB[0], axsB[1])
axsB[0].plot(range(0, 10), range(10))
axsB[1].plot(range(3, 13), range(10))
figB.suptitle('Join after creation, before plotting: x-axes limits are the same oin both axes.')

这是 matplotlib.pyplot 中的错误还是我的理解?

【问题讨论】:

  • 标记为重复,因为链接的答案已经告诉您您可能想要自动缩放。
  • 啊,谢谢。我忽略了另一个答案中的“你可能想要 ax.autoscale()”评论,不知道它的用途。

标签: python matplotlib


【解决方案1】:

ax.get_shared_x_axes().join(ax, ax2) 只是将 ax2 附加到 Grouper。您仍然需要自动缩放轴

ax.autoscale()

由于现在共享两个轴,因此自动缩放其中一个轴就足够了。

在第二个示例中,它按预期工作,因为默认情况下通过ax.plot 添加一条线会自动缩放轴。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多