matplotlib / matplotlib/matplotlib
[Bug]: Axes.cla() with sharex may wrongly reset the axes limit.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 23.2k
- Forks
- 8.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 66
Description
### 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
```Python
ax = plt.subplot(211)
ax2 = plt.subplot(212, sharex=ax)
ax.plot(range(50))
ax2.plot(range(50))
ax.cla()
plt.show()
```
### Actual outcome

### Expected outcome

### 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read lib/matplotlib/axes/_base.py at the linked Axes.cla() code, then run the supplied subplot reproduction with sharex. Trace how the shared-axis relationship is represented when B is shared to A, and make the behavior match the expected outcome without resetting the x-limit. Confirm the reproduction no longer changes the limit unexpectedly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100