@@ -636,6 +636,7 @@ def test_handler_numpoints():
636
636
def test_text_nohandler_warning ():
637
637
"""Test that Text artists with labels raise a warning"""
638
638
fig , ax = plt .subplots ()
639
+ ax .plot ([0 ], label = "mock data" )
639
640
ax .text (x = 0 , y = 0 , s = "text" , label = "label" )
640
641
with pytest .warns (UserWarning ) as record :
641
642
ax .legend ()
@@ -703,7 +704,7 @@ def test_legend_title_empty():
703
704
# it comes back as an empty string, and that it is not
704
705
# visible:
705
706
fig , ax = plt .subplots ()
706
- ax .plot (range (10 ))
707
+ ax .plot (range (10 ), label = "mock data" )
707
708
leg = ax .legend ()
708
709
assert leg .get_title ().get_text () == ""
709
710
assert not leg .get_title ().get_visible ()
@@ -736,7 +737,7 @@ def test_window_extent_cached_renderer():
736
737
737
738
def test_legend_title_fontprop_fontsize ():
738
739
# test the title_fontsize kwarg
739
- plt .plot (range (10 ))
740
+ plt .plot (range (10 ), label = "mock data" )
740
741
with pytest .raises (ValueError ):
741
742
plt .legend (title = 'Aardvark' , title_fontsize = 22 ,
742
743
title_fontproperties = {'family' : 'serif' , 'size' : 22 })
@@ -747,27 +748,27 @@ def test_legend_title_fontprop_fontsize():
747
748
748
749
fig , axes = plt .subplots (2 , 3 , figsize = (10 , 6 ))
749
750
axes = axes .flat
750
- axes [0 ].plot (range (10 ))
751
+ axes [0 ].plot (range (10 ), label = "mock data" )
751
752
leg0 = axes [0 ].legend (title = 'Aardvark' , title_fontsize = 22 )
752
753
assert leg0 .get_title ().get_fontsize () == 22
753
- axes [1 ].plot (range (10 ))
754
+ axes [1 ].plot (range (10 ), label = "mock data" )
754
755
leg1 = axes [1 ].legend (title = 'Aardvark' ,
755
756
title_fontproperties = {'family' : 'serif' , 'size' : 22 })
756
757
assert leg1 .get_title ().get_fontsize () == 22
757
- axes [2 ].plot (range (10 ))
758
+ axes [2 ].plot (range (10 ), label = "mock data" )
758
759
mpl .rcParams ['legend.title_fontsize' ] = None
759
760
leg2 = axes [2 ].legend (title = 'Aardvark' ,
760
761
title_fontproperties = {'family' : 'serif' })
761
762
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" )
763
764
leg3 = axes [3 ].legend (title = 'Aardvark' )
764
765
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" )
766
767
mpl .rcParams ['legend.title_fontsize' ] = 20
767
768
leg4 = axes [4 ].legend (title = 'Aardvark' ,
768
769
title_fontproperties = {'family' : 'serif' })
769
770
assert leg4 .get_title ().get_fontsize () == 20
770
- axes [5 ].plot (range (10 ))
771
+ axes [5 ].plot (range (10 ), label = "mock data" )
771
772
leg5 = axes [5 ].legend (title = 'Aardvark' )
772
773
assert leg5 .get_title ().get_fontsize () == 20
773
774
@@ -1071,6 +1072,7 @@ def test_legend_labelcolor_rcparam_markerfacecolor_short():
1071
1072
assert mpl .colors .same_color (text .get_color (), color )
1072
1073
1073
1074
1075
+ @pytest .mark .filterwarnings ("ignore:No artists with labels found to put in legend" )
1074
1076
def test_get_set_draggable ():
1075
1077
legend = plt .legend ()
1076
1078
assert not legend .get_draggable ()
@@ -1289,74 +1291,75 @@ def test_loc_invalid_tuple_exception():
1289
1291
fig , ax = plt .subplots ()
1290
1292
with pytest .raises (ValueError , match = ('loc must be string, coordinate '
1291
1293
'tuple, or an integer 0-10, not \\ (1.1,\\ )' )):
1292
- ax .legend (loc = (1.1 , ))
1294
+ ax .legend (loc = (1.1 , ), labels = [ "mock data" ] )
1293
1295
1294
1296
with pytest .raises (ValueError , match = ('loc must be string, coordinate '
1295
1297
'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" ] )
1297
1299
1298
1300
with pytest .raises (ValueError , match = ('loc must be string, coordinate '
1299
1301
'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" ] )
1301
1303
1302
1304
1303
1305
def test_loc_valid_tuple ():
1304
1306
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" ] )
1307
1309
1308
1310
1309
1311
def test_loc_valid_list ():
1310
1312
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" ] )
1313
1315
1314
1316
1315
1317
def test_loc_invalid_list_exception ():
1316
1318
fig , ax = plt .subplots ()
1317
1319
with pytest .raises (ValueError , match = ('loc must be string, coordinate '
1318
1320
'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" ] )
1320
1322
1321
1323
1322
1324
def test_loc_invalid_type ():
1323
1325
fig , ax = plt .subplots ()
1324
1326
with pytest .raises (ValueError , match = ("loc must be string, coordinate "
1325
1327
"tuple, or an integer 0-10, not {'not': True}" )):
1326
- ax .legend (loc = {'not' : True })
1328
+ ax .legend (loc = {'not' : True }, labels = [ "mock data" ] )
1327
1329
1328
1330
1329
1331
def test_loc_validation_numeric_value ():
1330
1332
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" ] )
1335
1337
with pytest .raises (ValueError , match = ('loc must be string, coordinate '
1336
1338
'tuple, or an integer 0-10, not 11' )):
1337
- ax .legend (loc = 11 )
1339
+ ax .legend (loc = 11 , labels = [ "mock data" ] )
1338
1340
1339
1341
with pytest .raises (ValueError , match = ('loc must be string, coordinate '
1340
1342
'tuple, or an integer 0-10, not -1' )):
1341
- ax .legend (loc = - 1 )
1343
+ ax .legend (loc = - 1 , labels = [ "mock data" ] )
1342
1344
1343
1345
1344
1346
def test_loc_validation_string_value ():
1345
1347
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 )
1358
1361
with pytest .raises (ValueError , match = "'wrong' is not a valid value for" ):
1359
- ax .legend (loc = 'wrong' )
1362
+ ax .legend (loc = 'wrong' , labels = labels )
1360
1363
1361
1364
1362
1365
def test_legend_handle_label_mismatch ():
@@ -1375,3 +1378,16 @@ def test_legend_handle_label_mismatch_no_len():
1375
1378
labels = iter (["pl1" , "pl2" , "pl3" ]))
1376
1379
assert len (legend .legend_handles ) == 2
1377
1380
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