You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: y_label in wrong place after clearing for twinx axes #28268
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
importmatplotlibimportmatplotlib.pyplotaspltfigure=plt.figure()
ax=figure.add_subplot(111)
ax2=ax.twinx()
ax2.cla()#or clear() ; doesn't work both waysax.set_ylabel('ax_label')
ax2.set_ylabel('ax2_label')#expected to be on the rightplt.show()
Actual outcome
Expected outcome
after clearing ax2 is still on the right, I expected its label also to be on the right
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
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.
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).
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.
Activity
tacaswell commentedon May 22, 2024
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,
claandclearare aliases for each other now but we will probably never remove either because the API thrash is not worth it.nwgruber commentedon 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 commentedon Sep 30, 2024
Hi! Is this bug still present? In case I could work on it
timhoffm commentedon Sep 30, 2024
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 commentedon Oct 2, 2024
Perfect, I got your point! I'll start working on it. Many thanks
Add test.py to test the various issues linked to the matplotlib#28268 …
jklymak commentedon 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 commentedon Oct 15, 2024
I'm not sure this solves all clear topics, but making a TwinAxes with dedicated semantics and an overwritten clear() method seems reasonable.
DasWolfle commentedon Jul 22, 2025
Any updates on this?
I just stumbled opon this bug on my current project.
timhoffm commentedon Jul 22, 2025
Obviously not. Updates would show up here or in the linked PRs.