matplotlib icon indicating copy to clipboard operation
matplotlib copied to clipboard

[Bug]: Axes.cla() with sharex may wrongly reset the axes limit.

Open tianzhuqiao opened this issue 1 year ago • 1 comments

Bug summary

For example, when subplot A shares the x-axis with subplot B (A is created with sharex=B), a call to B.cla() will set the xlimit to [0, 1], which may not be expected (in contrast, a call to A.cla() will not change the xlimit).

I think the main problem comes from the following function, where it will check whether its _sharex attribute is None; however, in this case, B is shared to A, but no axis is shared to B, so B's _sharex attribute is None. https://github.com/matplotlib/matplotlib/blob/9e18a343fb58a2978a8e27df03190ed21c61c343/lib/matplotlib/axes/_base.py#L1369

Code for reproduction

ax = plt.subplot(211)
ax2 = plt.subplot(212, sharex=ax)
ax.plot(range(50))
ax2.plot(range(50))
ax.cla()
plt.show()

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

No response

Matplotlib Version

3.8.3

Matplotlib Backend

'TkAgg'

Python version

3.9.13

Jupyter version

No response

Installation

pip

tianzhuqiao avatar Feb 27 '24 02:02 tianzhuqiao