@@ -636,6 +636,7 @@ def test_handler_numpoints():
636636def test_text_nohandler_warning ():
637637 """Test that Text artists with labels raise a warning"""
638638 fig , ax = plt .subplots ()
639+ ax .plot ([0 ], label = "mock data" )
639640 ax .text (x = 0 , y = 0 , s = "text" , label = "label" )
640641 with pytest .warns (UserWarning ) as record :
641642 ax .legend ()
@@ -703,7 +704,7 @@ def test_legend_title_empty():
703704 # it comes back as an empty string, and that it is not
704705 # visible:
705706 fig , ax = plt .subplots ()
706- ax .plot (range (10 ))
707+ ax .plot (range (10 ), label = "mock data" )
707708 leg = ax .legend ()
708709 assert leg .get_title ().get_text () == ""
709710 assert not leg .get_title ().get_visible ()
@@ -736,7 +737,7 @@ def test_window_extent_cached_renderer():
736737
737738def test_legend_title_fontprop_fontsize ():
738739 # test the title_fontsize kwarg
739- plt .plot (range (10 ))
740+ plt .plot (range (10 ), label = "mock data" )
740741 with pytest .raises (ValueError ):
741742 plt .legend (title = 'Aardvark' , title_fontsize = 22 ,
742743 title_fontproperties = {'family' : 'serif' , 'size' : 22 })
@@ -747,27 +748,27 @@ def test_legend_title_fontprop_fontsize():
747748
748749 fig , axes = plt .subplots (2 , 3 , figsize = (10 , 6 ))
749750 axes = axes .flat
750- axes [0 ].plot (range (10 ))
751+ axes [0 ].plot (range (10 ), label = "mock data" )
751752 leg0 = axes [0 ].legend (title = 'Aardvark' , title_fontsize = 22 )
752753 assert leg0 .get_title ().get_fontsize () == 22
753- axes [1 ].plot (range (10 ))
754+ axes [1 ].plot (range (10 ), label = "mock data" )
754755 leg1 = axes [1 ].legend (title = 'Aardvark' ,
755756 title_fontproperties = {'family' : 'serif' , 'size' : 22 })
756757 assert leg1 .get_title ().get_fontsize () == 22
757- axes [2 ].plot (range (10 ))
758+ axes [2 ].plot (range (10 ), label = "mock data" )
758759 mpl .rcParams ['legend.title_fontsize' ] = None
759760 leg2 = axes [2 ].legend (title = 'Aardvark' ,
760761 title_fontproperties = {'family' : 'serif' })
761762 assert leg2 .get_title ().get_fontsize () == mpl .rcParams ['font.size' ]
762- axes [3 ].plot (range (10 ))
763+ axes [3 ].plot (range (10 ), label = "mock data" )
763764 leg3 = axes [3 ].legend (title = 'Aardvark' )
764765 assert leg3 .get_title ().get_fontsize () == mpl .rcParams ['font.size' ]
765- axes [4 ].plot (range (10 ))
766+ axes [4 ].plot (range (10 ), label = "mock data" )
766767 mpl .rcParams ['legend.title_fontsize' ] = 20
767768 leg4 = axes [4 ].legend (title = 'Aardvark' ,
768769 title_fontproperties = {'family' : 'serif' })
769770 assert leg4 .get_title ().get_fontsize () == 20
770- axes [5 ].plot (range (10 ))
771+ axes [5 ].plot (range (10 ), label = "mock data" )
771772 leg5 = axes [5 ].legend (title = 'Aardvark' )
772773 assert leg5 .get_title ().get_fontsize () == 20
773774
@@ -1071,6 +1072,7 @@ def test_legend_labelcolor_rcparam_markerfacecolor_short():
10711072 assert mpl .colors .same_color (text .get_color (), color )
10721073
10731074
1075+ @pytest .mark .filterwarnings ("ignore:No artists with labels found to put in legend" )
10741076def test_get_set_draggable ():
10751077 legend = plt .legend ()
10761078 assert not legend .get_draggable ()
@@ -1289,74 +1291,75 @@ def test_loc_invalid_tuple_exception():
12891291 fig , ax = plt .subplots ()
12901292 with pytest .raises (ValueError , match = ('loc must be string, coordinate '
12911293 'tuple, or an integer 0-10, not \\ (1.1,\\ )' )):
1292- ax .legend (loc = (1.1 , ))
1294+ ax .legend (loc = (1.1 , ), labels = [ "mock data" ] )
12931295
12941296 with pytest .raises (ValueError , match = ('loc must be string, coordinate '
12951297 'tuple, or an integer 0-10, not \\ (0.481, 0.4227, 0.4523\\ )' )):
1296- ax .legend (loc = (0.481 , 0.4227 , 0.4523 ))
1298+ ax .legend (loc = (0.481 , 0.4227 , 0.4523 ), labels = [ "mock data" ] )
12971299
12981300 with pytest .raises (ValueError , match = ('loc must be string, coordinate '
12991301 'tuple, or an integer 0-10, not \\ (0.481, \' go blue\' \\ )' )):
1300- ax .legend (loc = (0.481 , "go blue" ))
1302+ ax .legend (loc = (0.481 , "go blue" ), labels = [ "mock data" ] )
13011303
13021304
13031305def test_loc_valid_tuple ():
13041306 fig , ax = plt .subplots ()
1305- ax .legend (loc = (0.481 , 0.442 ))
1306- ax .legend (loc = (1 , 2 ))
1307+ ax .legend (loc = (0.481 , 0.442 ), labels = [ "mock data" ] )
1308+ ax .legend (loc = (1 , 2 ), labels = [ "mock data" ] )
13071309
13081310
13091311def test_loc_valid_list ():
13101312 fig , ax = plt .subplots ()
1311- ax .legend (loc = [0.481 , 0.442 ])
1312- ax .legend (loc = [1 , 2 ])
1313+ ax .legend (loc = [0.481 , 0.442 ], labels = [ "mock data" ] )
1314+ ax .legend (loc = [1 , 2 ], labels = [ "mock data" ] )
13131315
13141316
13151317def test_loc_invalid_list_exception ():
13161318 fig , ax = plt .subplots ()
13171319 with pytest .raises (ValueError , match = ('loc must be string, coordinate '
13181320 'tuple, or an integer 0-10, not \\ [1.1, 2.2, 3.3\\ ]' )):
1319- ax .legend (loc = [1.1 , 2.2 , 3.3 ])
1321+ ax .legend (loc = [1.1 , 2.2 , 3.3 ], labels = [ "mock data" ] )
13201322
13211323
13221324def test_loc_invalid_type ():
13231325 fig , ax = plt .subplots ()
13241326 with pytest .raises (ValueError , match = ("loc must be string, coordinate "
13251327 "tuple, or an integer 0-10, not {'not': True}" )):
1326- ax .legend (loc = {'not' : True })
1328+ ax .legend (loc = {'not' : True }, labels = [ "mock data" ] )
13271329
13281330
13291331def test_loc_validation_numeric_value ():
13301332 fig , ax = plt .subplots ()
1331- ax .legend (loc = 0 )
1332- ax .legend (loc = 1 )
1333- ax .legend (loc = 5 )
1334- ax .legend (loc = 10 )
1333+ ax .legend (loc = 0 , labels = [ "mock data" ] )
1334+ ax .legend (loc = 1 , labels = [ "mock data" ] )
1335+ ax .legend (loc = 5 , labels = [ "mock data" ] )
1336+ ax .legend (loc = 10 , labels = [ "mock data" ] )
13351337 with pytest .raises (ValueError , match = ('loc must be string, coordinate '
13361338 'tuple, or an integer 0-10, not 11' )):
1337- ax .legend (loc = 11 )
1339+ ax .legend (loc = 11 , labels = [ "mock data" ] )
13381340
13391341 with pytest .raises (ValueError , match = ('loc must be string, coordinate '
13401342 'tuple, or an integer 0-10, not -1' )):
1341- ax .legend (loc = - 1 )
1343+ ax .legend (loc = - 1 , labels = [ "mock data" ] )
13421344
13431345
13441346def test_loc_validation_string_value ():
13451347 fig , ax = plt .subplots ()
1346- ax .legend (loc = 'best' )
1347- ax .legend (loc = 'upper right' )
1348- ax .legend (loc = 'best' )
1349- ax .legend (loc = 'upper right' )
1350- ax .legend (loc = 'upper left' )
1351- ax .legend (loc = 'lower left' )
1352- ax .legend (loc = 'lower right' )
1353- ax .legend (loc = 'right' )
1354- ax .legend (loc = 'center left' )
1355- ax .legend (loc = 'center right' )
1356- ax .legend (loc = 'lower center' )
1357- ax .legend (loc = 'upper center' )
1348+ labels = ["mock data" ]
1349+ ax .legend (loc = 'best' , labels = labels )
1350+ ax .legend (loc = 'upper right' , labels = labels )
1351+ ax .legend (loc = 'best' , labels = labels )
1352+ ax .legend (loc = 'upper right' , labels = labels )
1353+ ax .legend (loc = 'upper left' , labels = labels )
1354+ ax .legend (loc = 'lower left' , labels = labels )
1355+ ax .legend (loc = 'lower right' , labels = labels )
1356+ ax .legend (loc = 'right' , labels = labels )
1357+ ax .legend (loc = 'center left' , labels = labels )
1358+ ax .legend (loc = 'center right' , labels = labels )
1359+ ax .legend (loc = 'lower center' , labels = labels )
1360+ ax .legend (loc = 'upper center' , labels = labels )
13581361 with pytest .raises (ValueError , match = "'wrong' is not a valid value for" ):
1359- ax .legend (loc = 'wrong' )
1362+ ax .legend (loc = 'wrong' , labels = labels )
13601363
13611364
13621365def test_legend_handle_label_mismatch ():
@@ -1375,3 +1378,16 @@ def test_legend_handle_label_mismatch_no_len():
13751378 labels = iter (["pl1" , "pl2" , "pl3" ]))
13761379 assert len (legend .legend_handles ) == 2
13771380 assert len (legend .get_texts ()) == 2
1381+
1382+
1383+ def test_legend_nolabels_warning ():
1384+ plt .plot ([1 , 2 , 3 ])
1385+ with pytest .raises (UserWarning , match = "No artists with labels found" ):
1386+ plt .legend ()
1387+
1388+
1389+ @pytest .mark .filterwarnings ("ignore:No artists with labels found to put in legend" )
1390+ def test_legend_nolabels_draw ():
1391+ plt .plot ([1 , 2 , 3 ])
1392+ plt .legend ()
1393+ assert plt .gca ().get_legend () is not None
0 commit comments