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

Skip to content

Commit f23a8de

Browse files
authored
Merge pull request #22440 from timhoffm/legend-message
Clarify warning about labels with leading underscores.
2 parents 7de9e84 + 8573a52 commit f23a8de

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/matplotlib/legend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ def val_or_rc(val, rc_name):
406406
_lab, _hand = [], []
407407
for label, handle in zip(labels, handles):
408408
if isinstance(label, str) and label.startswith('_'):
409-
_api.warn_external('The handle {!r} has a label of {!r} '
410-
'which cannot be automatically added to'
411-
' the legend.'.format(handle, label))
409+
_api.warn_external(f"The label {label!r} of {handle!r} starts "
410+
"with '_'. It is thus excluded from the "
411+
"legend.")
412412
else:
413413
_lab.append(label)
414414
_hand.append(handle)

lib/matplotlib/tests/test_legend.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ def test_various_labels():
7878
ax.legend(numpoints=1, loc='best')
7979

8080

81+
def test_legend_label_with_leading_underscore():
82+
"""
83+
Test that artists with labels starting with an underscore are not added to
84+
the legend, and that a warning is issued if one tries to add them
85+
explicitly.
86+
"""
87+
fig, ax = plt.subplots()
88+
line, = ax.plot([0, 1], label='_foo')
89+
with pytest.warns(UserWarning,
90+
match=r"starts with '_'.*excluded from the legend."):
91+
legend = ax.legend(handles=[line])
92+
assert len(legend.legendHandles) == 0
93+
94+
8195
@image_comparison(['legend_labels_first.png'], remove_text=True)
8296
def test_labels_first():
8397
# test labels to left of markers

0 commit comments

Comments
 (0)