0

I'm trying to share matplotlib axes after (example axsA) they've been plotted (not just after they've been created). Contrary to my expectation, after doing the join, the axes do not get redrawn with shared xlims, even if I do plt.draw() and plt.show():

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()

Join two axes after plotting: x-axes limits are not the same in the two axes.

I seem able to achieve shared axes only if I join them in a Grouper object before plotting:

# 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.')

Join two axes after creation, before plotting: x-axes limits are the same oin both axes.

Is this a bug in matplotlib.pyplot or in my understanding?

EEE
  • 501
  • 6
  • 13

1 Answers1

1

ax.get_shared_x_axes().join(ax, ax2) just appends ax2 to the Grouper. You still need to autoscale the axes

ax.autoscale()

Because both axes are shared now, it is sufficient to autoscale one of the axes.

In the second example it works as expected, because adding a line via ax.plot autoscales the axes automatically by default.

Sheldore
  • 37,862
  • 7
  • 57
  • 71
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712