Skip to content

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

Closed

Description

@dstansby

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

Activity

  1. ksunden commented on Mar 24, 2024

    @ksunden
    Member

    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.

  2. dstansby commented on Mar 24, 2024

    @dstansby
    MemberAuthor

    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...)?

  3. timhoffm commented on Mar 24, 2024

    @timhoffm
    Member

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

  4. self-assigned this
    on Mar 24, 2024
  5. added
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    on Mar 24, 2024
  6. timhoffm commented on Mar 24, 2024

    @timhoffm
    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.
  7. added 4 commits that reference this issue on Jul 17, 2024
    15bc099
    026d3c7
    f848210
    2a4710a
  8. added 3 commits that reference this issue on Sep 20, 2024
    8000fcc
    8220f15
    bba0678
  9. added a commit that references this issue on Sep 30, 2024
    537ea7a
  10. added this to the v3.10.0 milestone on Oct 1, 2024
  11. added a commit that references this issue on Oct 10, 2024
    ef38f68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions