matplotlib / matplotlib/matplotlib

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

Offen
#27,825 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic: axes
Vorherrschende Sprache
Python
Sterne
23.2k
Forks
8.5k
Ø Merge
1 T. 6 Std.
Gemergte PRs (30 T.)
66

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.