Skip to content

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

Open

Description

@katetsisar

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

Activity

  1. tacaswell commented on May 22, 2024

    @tacaswell
    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.

  2. added this to the v3.10.0 milestone on May 22, 2024
  3. nwgruber commented on Jun 1, 2024

    @nwgruber

    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.

  4. dberardi99 commented on Sep 30, 2024

    @dberardi99

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

  5. timhoffm commented on Sep 30, 2024

    @timhoffm
    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).

  6. dberardi99 commented on Oct 2, 2024

    @dberardi99

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

  7. jklymak commented on Oct 14, 2024

    @jklymak
    Member

    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.

  8. timhoffm commented on Oct 15, 2024

    @timhoffm
    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.

  9. modified the milestones: v3.10.0, v3.11.0 on Oct 21, 2024
  10. DasWolfle commented on Jul 22, 2025

    @DasWolfle

    Any updates on this?
    I just stumbled opon this bug on my current project.

  11. timhoffm commented on Jul 22, 2025

    @timhoffm
    Member

    Obviously not. Updates would show up here or in the linked PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions