File tree Expand file tree Collapse file tree 2 files changed +17
-3
lines changed
Expand file tree Collapse file tree 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):
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 )
Original file line number Diff line number Diff 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 )
8296def test_labels_first ():
8397 # test labels to left of markers
You can’t perform that action at this time.
0 commit comments