10000 Deprecate non-string values as legend labels · matplotlib/matplotlib@9b7d171 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b7d171

Browse files
committed
Deprecate non-string values as legend labels
1 parent b42d6d9 commit 9b7d171

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ Support for passing ``None`` as base class to `.axes.subplot_class_factory`,
327327
``axes_grid1.parasite_axes.parasite_axes_auxtrans_class_factory`` is deprecated.
328328
Explicitly pass the correct base ``Axes`` class instead.
329329

330+
Legend labels must be strings
331+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
332+
Passing non-string objects as legend labels is deprecated.
333+
330334
``axes_rgb``
331335
~~~~~~~~~~~~
332336
In :mod:`mpl_toolkits.axes_grid1.axes_rgb`, ``imshow_rgb`` is deprecated (use

lib/matplotlib/legend.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ def __init__(self, parent, handles, labels,
395395
# trim handles and labels if illegal label...
396396
_lab, _hand = [], []
397397
for label, handle in zip(labels, handles):
398+
if not isinstance(label, str):
399+
cbook.warn_deprecated(
400+
"3.3",
401+
message="Passing non-string objects as legend "
402+
"labels is deprecated.")
398403
if isinstance(label, str) and label.startswith('_'):
399404
cbook._warn_external('The handle {!r} has a label of {!r} '
400405
'which cannot be automatically added to'

lib/matplotlib/tests/test_legend.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
import pytest
77

8+
from matplotlib.cbook import MatplotlibDeprecationWarning
89
from matplotlib.testing.decorators import image_comparison
910
import matplotlib.pyplot as plt
1011
import matplotlib as mpl
@@ -234,6 +235,15 @@ def test_legend_handle_label(self):
234235
plt.legend(lines, ['hello world'])
235236
Legend.assert_called_with(plt.gca(), lines, ['hello world'])
236237

238+
def test_legend_handles_only(self):
239+
lines = plt.plot(range(10))
240+
with pytest.warns(MatplotlibDeprecationWarning,
241+
match="Passing non-string objects as legend labels is "
242+
"deprecated"):
243+
# a single arg is interpreted as labels
244+
# it's a common error to just pass handles
245+
plt.legend(lines)
246+
237247
def test_legend_no_args(self):
238248
lines = plt.plot(range(10), label='hello world')
239249
with mock.patch('matplotlib.legend.Legend') as Legend:

0 commit comments

Comments
 (0)
0