File tree 2 files changed +17
-3
lines changed
2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -406,9 +406,9 @@ def val_or_rc(val, rc_name):
406
406
_lab , _hand = [], []
407
407
for label , handle in zip (labels , handles ):
408
408
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." )
412
412
else :
413
413
_lab .append (label )
414
414
_hand .append (handle )
Original file line number Diff line number Diff line change @@ -78,6 +78,20 @@ def test_various_labels():
78
78
ax .legend (numpoints = 1 , loc = 'best' )
79
79
80
80
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
+
81
95
@image_comparison (['legend_labels_first.png' ], remove_text = True )
82
96
def test_labels_first ():
83
97
# test labels to left of markers
You can’t perform that action at this time.
0 commit comments