Skip to content

[Bug]: y_label in wrong place after clearing for twinx axes #28268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
katetsisar opened this issue May 21, 2024 · 7 comments · May be fixed by #28325 or #28974
Open

[Bug]: y_label in wrong place after clearing for twinx axes #28268

katetsisar opened this issue May 21, 2024 · 7 comments · May be fixed by #28325 or #28974

Comments

@katetsisar
Copy link

Bug summary

After making sexond axes on the righr with twinx y_label for this second axes is on the right, but clearing (doesn't matter is it cla() or clear()) second axes breaks set_ylabel putting it on the left. Ticks for second axes are still on the right, so I don't see why y_label wouldn't also go there (thats why I think its a bug)

Code for reproduction

import matplotlib
import matplotlib.pyplot as plt

figure = plt.figure()
ax = figure.add_subplot(111)
ax2 = ax.twinx()
ax2.cla()#or clear() ; doesn't work both ways
ax.set_ylabel('ax_label')
ax2.set_ylabel('ax2_label')#expected to be on the right
plt.show()

Actual outcome

image

Expected outcome

after clearing ax2 is still on the right, I expected its label also to be on the right
image

Additional information

I saw that similar solved problems occurred with twinx()(it was something to do with mirroring), but I couldn't find solution for this specific situation

Operating system

No response

Matplotlib Version

3.7.1

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

@tacaswell
Copy link
Member

xref #23839 and #27825

The core problem is that there is some ambiguity as to what "Clearing" as axes means and it is double ambiguous with shared axis.


As a side note, cla and clear are aliases for each other now but we will probably never remove either because the API thrash is not worth it.

@nwgruber
Copy link

nwgruber commented Jun 1, 2024

I noticed the same problem using FigureCanvasQTAgg in an app I'm working. Version 3.7.0 and up all have this problem - I tested 3.7.0, 3.7.3, 3.8.3, and 3.9.0. In previous versions (I tested 3.5.1 and 3.6.3) the second y label remains on the correct side after clearing the axes and plotting.

@dberardi99
Copy link

Hi! Is this bug still present? In case I could work on it

@timhoffm
Copy link
Member

The bug is present and there's an open pull request #28325.

If you want to contribute, please start from there. In particular, it should be analyzed which aspects of twinned axes are reset and which are already kept under clear(). See #28325 (review).

@dberardi99
Copy link

Perfect, I got your point! I'll start working on it. Many thanks

@jklymak
Copy link
Member

jklymak commented Oct 14, 2024

In my opinion "clear" is not too mysterious - it puts the axes in the pristine state that Matplotlib creates it in. The difficulty here is that from the users point of view, Matplotlib creates the twin axes with the labels on the opposite side of a normal axes. To me, that says we should return to the user a slightly modified axes subclass that explicitly swaps the default axes position.

@timhoffm
Copy link
Member

To me, that says we should return to the user a slightly modified axes subclass that explicitly swaps the default axes position.

I'm not sure this solves all clear topics, but making a TwinAxes with dedicated semantics and an overwritten clear() method seems reasonable.

@timhoffm timhoffm modified the milestones: v3.10.0, v3.11.0 Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment