matplotlib / matplotlib/matplotlib

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

Open
#27,825 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: axes
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

![image](https://github.com/matplotlib/matplotlib/assets/12435643/78bf5ac4-c62a-4556-bb9c-51ae7d0cadf0)

### Expected outcome

![image](https://github.com/matplotlib/matplotlib/assets/12435643/5b22ee2f-473a-4077-b40d-9a609c698ac8)

### 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.