8000 Deprecate non-string values as legend labels by timhoffm · Pull Request #16771 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Deprecate non-string values as legend labels #16771

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ Support for passing ``None`` as base class to `.axes.subplot_class_factory`,
``axes_grid1.parasite_axes.parasite_axes_auxtrans_class_factory`` is deprecated.
Explicitly pass the correct base ``Axes`` class instead.

Legend labels must be strings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Passing non-string objects as legend labels is deprecated.

``axes_rgb``
~~~~~~~~~~~~
In :mod:`mpl_toolkits.axes_grid1.axes_rgb`, ``imshow_rgb`` is deprecated (use
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ def __init__(self, parent, handles, labels,
# trim handles and labels if illegal label...
_lab, _hand = [], []
for label, handle in zip(labels, handles):
if not isinstance(label, str):
cbook.warn_deprecated(
"3.3",
message="Passing non-string objects as legend "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a note suggesting that they probably wanted to pass in two lists?

Copy link
Member Author
@timhoffm timhoffm Mar 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not. This check is now implemented in Legend with signature Legend(parent, handles, labels, ..). So the "two lists" does not apply anymore. Otherwise I'd have to make the check in mlegend._parse_legend_args(). But then the parameter in Legend is not checked explicitly - was not before, but I think it should anyway. Furthermore, you can pass labels positionally or by kwarg to legend(); while the str-check would apply to both, the proposed addition would only make sense for the former.

On the other hand, IMHO it's clear enough to respond on a call legend(lines) with the message Passing non-string objects as legend labels is deprecated. It's rather obvious from this that we interpret the only parameter as labels.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that we are still going to get some confused users, but this is certainly clearer than it currently is!

"labels is deprecated.")
if isinstance(label, str) and label.startswith('_'):
cbook._warn_external('The handle {!r} has a label of {!r} '
'which cannot be automatically added to'
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import pytest

from matplotlib.cbook import MatplotlibDeprecationWarning
from matplotlib.testing.decorators import image_comparison
import matplotlib.pyplot as plt
import matplotlib as mpl
Expand Down Expand Up @@ -234,6 +235,15 @@ def test_legend_handle_label(self):
plt.legend(lines, ['hello world'])
Legend.assert_called_with(plt.gca(), lines, ['hello world'])

def test_legend_handles_only(self):
lines = plt.plot(range(10))
with pytest.warns(MatplotlibDeprecationWarning,
match="Passing non-string objects as legend labels "
"is deprecated"):
# a single arg is interpreted as labels
# it's a common error to just pass handles
plt.legend(lines)

def test_legend_no_args(self):
lines = plt.plot(range(10), label='hello world')
with mock.patch('matplotlib.legend.Legend') as Legend:
Expand Down
0