Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf23a8de

Browse files
authored
Merge pull request#22440 from timhoffm/legend-message
Clarify warning about labels with leading underscores.
2 parents7de9e84 +8573a52 commitf23a8de

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
forlabel,handleinzip(labels,handles):
408408
ifisinstance(label,str)andlabel.startswith('_'):
409-
_api.warn_external('Thehandle {!r}has a labelof {!r}'
410-
'which cannot be automatically added to'
411-
' thelegend.'.format(handle,label))
409+
_api.warn_external(f"Thelabel{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+
deftest_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+
withpytest.warns(UserWarning,
90+
match=r"starts with '_'.*excluded from the legend."):
91+
legend=ax.legend(handles=[line])
92+
assertlen(legend.legendHandles)==0
93+
94+
8195
@image_comparison(['legend_labels_first.png'],remove_text=True)
8296
deftest_labels_first():
8397
# test labels to left of markers

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp