Skip to content

Commit

Permalink
MNT: Prevent users from erroneously using legend label API on Axis
Browse files Browse the repository at this point in the history
Closes matplotlib#27971.

For a complete explanation see matplotlib#27971 (comment)
  • Loading branch information
timhoffm committed Jul 17, 2024
1 parent 199c31f commit f848210
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/deprecations/28584-TH.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``Axis.get_label``
~~~~~~~~~~~~~~~~~~
`.Artist.get_label` is reserved for legend labels. `.Axis.get_label` had overwritten
this, providing the axis label `.Text` instance. `.Axis.get_label` will raise a
RuntimeError in the future. If you want the axis label `.Text` instance, use the
property `.Axis.label` instead.
9 changes: 9 additions & 0 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,15 @@ def get_gridlines(self):
return cbook.silent_list('Line2D gridline',
[tick.gridline for tick in ticks])

def set_label(self, s):
"""Not supported. Raises RuntimeError."""
raise RuntimeError(
"A legend label cannot be assigned to an Axis. Did you mean to "
"set the axis label via set_label_text()?")

@_api.deprecated("3.10", message="Use the property 'Axis.label' instead")
# Note: After the deprecation, this should raise a RuntimeError; see
# https://github.com/matplotlib/matplotlib/issues/27971#issuecomment-2016955731
def get_label(self):
"""Return the axis label as a Text instance."""
return self.label
Expand Down

0 comments on commit f848210

Please sign in to comment.