@@ -1393,15 +1393,33 @@ def test_legend_nolabels_draw():
1393
1393
assert plt .gca ().get_legend () is not None
1394
1394
1395
1395
1396
- def test_legend_key_patch ():
1397
- """Test that legend key entries are patches"""
1396
+ def test_boxplot_legend ():
1397
+ # Test that boxplot legends handles are patches
1398
+ # and labels are generated from boxplot's labels parameter.
1398
1399
fig , axs = plt .subplots ()
1399
1400
A = 5 * np .random .rand (100 , 1 )
1400
1401
B = 10 * np .random .rand (100 , 1 ) - 5
1401
1402
C = 7 * np .random .rand (100 , 1 ) - 5
1402
- bp0 = axs .boxplot (A , positions = [0 ], patch_artist = True )
1403
- bp1 = axs .boxplot (B , positions = [1 ], patch_artist = True )
1404
- bp2 = axs .boxplot (C , positions = [2 ], patch_artist = True )
1405
- legend = axs .legend (['A' , 'B' , 'C' ])
1403
+ labels = ['a' , 'b' , 'c' ]
1404
+
1405
+ bp0 = axs .boxplot (A , positions = [0 ], patch_artist = True , labels = labels [0 ])
1406
+ bp1 = axs .boxplot (B , positions = [1 ], patch_artist = True , labels = labels [1 ])
1407
+ bp2 = axs .boxplot (C , positions = [2 ], patch_artist = True , labels = labels [2 ])
1408
+ # red, blue, green
1409
+ colors = [(1.0 , 0.0 , 0.0 , 1 ), (0.0 , 0.0 , 1.0 , 1 ), (0.0 , 0.5 , 0.0 , 1 )]
1410
+ box_list = [bp0 , bp1 , bp2 ]
1411
+ # Set colors to the boxes
1412
+ lbl_index = 0
1413
+ for b_plot , color in zip (box_list , colors ):
1414
+ for patch in b_plot ['boxes' ]:
1415
+ patch .set_color (color )
1416
+ lbl_index += 1
1417
+
1418
+ legend = axs .legend ()
1419
+ index = 0
1406
1420
for i in legend .legend_handles :
1407
1421
assert isinstance (i , mpl .patches .Rectangle )
1422
+ assert i .get_facecolor () == colors [index ]
1423
+ assert i .get_edgecolor () == colors [index ]
1424
+ assert i .get_label () == labels [index ]
1425
+ index += 1
0 commit comments