Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1de25cb

Browse files
authored
Merge pull request #26190 from anntzer/_l
Deprecate removal of explicit legend handles whose label starts with _.
2 parents c6f455b + 2f8a08b commit 1de25cb

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Artists explicitly passed in will no longer be filtered by legend() based on their label
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Currently, artists explicitly passed to ``legend(handles=[...])`` are filtered
4+
out if their label starts with an underscore. This behavior is deprecated;
5+
explicitly filter out such artists
6+
(``[art for art in artists if not art.get_label().startswith('_')]``) if
7+
necessary.

lib/matplotlib/legend.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,12 @@ def val_or_rc(val, rc_name):
465465
_lab, _hand = [], []
466466
for label, handle in zip(labels, handles):
467467
if isinstance(label, str) and label.startswith('_'):
468-
_api.warn_external(f"The label {label!r} of {handle!r} starts "
469-
"with '_'. It is thus excluded from the "
470-
"legend.")
468+
_api.warn_deprecated("3.8", message=(
469+
"An artist whose label starts with an underscore was passed to "
470+
"legend(); such artists will no longer be ignored in the future. "
471+
"To suppress this warning, explicitly filter out such artists, "
472+
"e.g. with `[art for art in artists if not "
473+
"art.get_label().startswith('_')]`."))
471474
else:
472475
_lab.append(label)
473476
_hand.append(handle)

lib/matplotlib/tests/test_legend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import matplotlib.lines as mlines
1818
from matplotlib.legend_handler import HandlerTuple
1919
import matplotlib.legend as mlegend
20-
from matplotlib import rc_context
20+
from matplotlib import _api, rc_context
2121
from matplotlib.font_manager import FontProperties
2222

2323

@@ -144,8 +144,7 @@ def test_legend_label_with_leading_underscore():
144144
"""
145145
fig, ax = plt.subplots()
146146
line, = ax.plot([0, 1], label='_foo')
147-
with pytest.warns(UserWarning,
148-
match=r"starts with '_'.*excluded from the legend."):
147+
with pytest.warns(_api.MatplotlibDeprecationWarning, match="with an underscore"):
149148
legend = ax.legend(handles=[line])
150149
assert len(legend.legend_handles) == 0
151150

0 commit comments

Comments
 (0)