Skip to content

[Bug]: ax.xaxis.set_label(...) doesn't set the x-axis label #27971

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

Closed
dstansby opened this issue Mar 24, 2024 · 4 comments · Fixed by #28584
Closed

[Bug]: ax.xaxis.set_label(...) doesn't set the x-axis label #27971

dstansby opened this issue Mar 24, 2024 · 4 comments · Fixed by #28584
Assignees
Milestone

Comments

@dstansby
Copy link
Member

Bug summary

I'm used to using the Axis API like ax.xaxis.set_major_formatter(...) etc., so to be consistent it would be nice if one could do ax.xaxis.set_label(...) instead of ax.set_xlabel(...). It seems like ax.xaxis.set_label(...) does not label the axis however.

Code for reproduction

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.xaxis.set_label("x-label")

Actual outcome

Screenshot 2024-03-24 at 10 02 10

Expected outcome

Screenshot 2024-03-24 at 10 02 26

Additional information

No response

Operating system

No response

Matplotlib Version

3.8.3

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

conda

@ksunden
Copy link
Member

ksunden commented Mar 24, 2024

The method you are looking for is set_label_text

set_label is the inherited method from Artist which provides the text that would appear in a legend entry (odd to do for Axis, but is defined by Artist)

However, confusingly, get_label is actually overridden with a different meaning in Axis, where it returns the Text instance of the label. (And so I might expect the setter to error with a raw string, but expect a Text instance)

I will state that the "_text" postfix is itself confusing, because it is given a string and is opposed to the version which uses Text.

@dstansby
Copy link
Member Author

Thanks - I thought something like that might be going on. Shall we close as won't fix then? Or we could keep open and as an action deprecate the overriden get_text() in favour of get_label_text() (if that would even be technically possible...)?

@timhoffm
Copy link
Member

Please leave open. I will think about whether we can make this less confusing when I have a quiet minute.

@timhoffm timhoffm self-assigned this Mar 24, 2024
@timhoffm
Copy link
Member

The name label is very broad and used for different things in different contexts. It would have been better to either be more specific set/get_legend_label or distinguish data and decoration Artist categories, which could have individual interpretations of label. But we are not in a place to change either. To limit misuse and confusion I propose to:

  • Override set_label() in Axis and raise RuntimeError("A legend label cannot be assigned to an Axis"). While this explicitly breaks Liskov's Substitution Principle, that principle was already implicitly broken by the design, i.e. deriving Axis as a class that cannot have legend entries from the Artist class that assumes it can have legend labels. IMHO it's better to make this explicit and fail loudly instead of silently accepting a value and not ever doing anything with it.
  • Deprecate the overridden get_label(). We should not be overwriting this with different semantics than the base class. Instead, suggest to access the attribute Axis.label directly. Whether that's a good pattern is a differerent topic. It's already there as an interface, so it won't go away too easily. Alternatively, one could add get_axislabel() (consistent with the existing Axis.get_ticklabels().
    When the deprecation expires, either fall back to the original behavior or, preferrably also raise a RuntimeError.

timhoffm added a commit to timhoffm/matplotlib that referenced this issue Jul 17, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Jul 17, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Jul 17, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Jul 17, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Sep 20, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Sep 20, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Sep 21, 2024
@QuLogic QuLogic added this to the v3.10.0 milestone Oct 1, 2024
kyracho pushed a commit to kyracho/matplotlib that referenced this issue Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants