@@ -858,7 +858,7 @@ def test_legend_pathcollection_labelcolor_linecolor():
858
858
859
859
leg = ax .legend (labelcolor = 'linecolor' )
860
860
for text , color in zip (leg .get_texts (), ['r' , 'g' , 'b' ]):
861
- assert mpl .colors .same_color (text .get_color (), color )
861
+ assert mpl .colors .same_color (text .get_color (), 'black' )
862
862
863
863
864
864
def test_legend_pathcollection_labelcolor_linecolor_iterable ():
@@ -1476,31 +1476,93 @@ def test_boxplot_legend_labels():
1476
1476
assert bp4 ['medians' ][0 ].get_label () == 'box A'
1477
1477
assert all (x .get_label ().startswith ("_" ) for x in bp4 ['medians' ][1 :])
1478
1478
1479
- """
1480
- def test_legend_labelcolor_linecolor_default_artists():
1481
- fig, axes = plt.subplots(2, 2)
1482
- x = np.random.randn(1000)
1483
- y = np.random.randn(1000)
1484
1479
1485
- # Top Left: Filled Histogram (default color C0)
1486
- axes[0, 0].hist(x, histtype='bar', label="spam")
1487
- leg00 = axes[0, 0].legend(labelcolor='linecolor')
1488
- assert np.allclose(mcolors.to_rgb(leg00.get_texts()[0].get_color()), mcolors.to_rgb('C0'))
1480
+ @pytest .mark .parametrize (
1481
+ "artist_type, plot_func, kwargs, expected_color" ,
1482
+ [
1483
+ # Plot with visible face color
1484
+ (
1485
+ "plot" , plt .plot ,
1486
+ {'c' : 'orange' },
1487
+ 'orange'
1488
+ ),
1489
+
1490
+ # Plot with marker edge only
1491
+ (
1492
+ "plot" , plt .plot ,
1493
+ {'mfc' : 'None' , 'mec' : 'red' },
1494
+ 'red'
1495
+ ),
1496
+
1497
+ # Plot with marker face only
1498
+ (
1499
+ "plot" , plt .plot ,
1500
+ {'mfc' : 'cyan' , 'mec' : 'None' },
1501
+ 'cyan'
1502
+ ),
1503
+
1504
+ # Scatter with edge color
1505
+ (
1506
+ "scatter" , plt .scatter ,
1507
+ {'facecolors' : 'None' , 'edgecolors' : 'blue' },
1508
+ 'blue'
1509
+ ),
1510
+
1511
+ # Scatter with face color
1512
+ (
1513
+ "scatter" , plt .scatter ,
1514
+ {'facecolors' : 'magenta' , 'edgecolors' : 'None' },
1515
+ 'magenta'
1516
+ ),
1517
+
1518
+ # Histogram bar (should fallback to black if ambiguous)
1519
+ (
1520
+ "hist" , plt .hist ,
1521
+ {'bins' : 10 , 'color' : 'C1' , 'histtype' : 'bar' },
1522
+ 'black'
1523
+ ),
1524
+
1525
+ # Histogram step (edgecolor)
1526
+ (
1527
+ "hist" , plt .hist ,
1528
+ {'bins' : 10 , 'color' : 'C2' , 'histtype' : 'step' },
1529
+ 'C2'
1530
+ ),
1531
+
1532
+ # Fully transparent (should not override default)
1533
+ (
1534
+ "plot" , plt .plot ,
1535
+ {'color' : (1 , 0 , 0 , 0 )},
1536
+ 'none'
1537
+ ),
1538
+ ]
1539
+ )
1540
+ def test_legend_labelcolor_linecolor_variants (
1541
+ artist_type , plot_func , kwargs , expected_color
1542
+ ):
1543
+ x = np .linspace (0 , 1 , 10 )
1544
+ y = np .random .randn (10 )
1545
+ fig , ax = plt .subplots ()
1546
+
1547
+ if artist_type == "hist" :
1548
+ plot_func (np .random .randn (100 ), label = "example" , ** kwargs )
1549
+ else :
1550
+ plot_func (x , y , label = "example" , ** kwargs )
1489
1551
1490
- # Top Right: Step Histogram (default color C0)
1491
- axes[0, 1].hist(x, histtype='step', label="spam")
1492
- leg01 = axes[0, 1].legend(labelcolor='linecolor')
1493
- assert np.allclose(mcolors.to_rgb(leg01.get_texts()[0].get_color()), mcolors.to_rgb('C0'))
1552
+ leg = ax .legend (labelcolor = 'linecolor' )
1553
+ label_color = leg .get_texts ()[0 ].get_color ()
1494
1554
1495
- # Bottom Left: Scatter (filled, default color C0)
1496
- axes[1, 0].scatter(x, y, label="spam")
1497
- leg10 = axes[1, 0].legend(labelcolor='linecolor')
1498
- assert np.allclose(mcolors.to_rgb(leg10.get_texts()[0].get_color()), mcolors.to_rgb('C0'))
1555
+ if isinstance (label_color , tuple ) and len (label_color ) == 4 :
1556
+ label_color = label_color [:3 ]
1499
1557
1500
- # Bottom Right: Scatter (outline, default edge color C0)
1501
- axes[1, 1].scatter(x, y, label="spam")
1502
- leg11 = axes[1, 1].legend(labelcolor='linecolor')
1503
- assert np.allclose(mcolors.to_rgb(leg11.get_texts()[0].get_color()), mcolors.to_rgb('C0'))
1558
+ if expected_color == 'none' :
1559
+ assert label_color == 'none' , (
1560
+ f"Expected 'none', got { label_color } for { artist_type } "
1561
+ )
1562
+ else :
1563
+ assert mcolors .same_color (label_color , expected_color ), (
1564
+ f"Expected: { expected_color } , Got: { label_color } "
1565
+ f"for artist_type={ artist_type } , kwargs={ kwargs } "
1566
+ )
1504
1567
1505
1568
plt .close (fig )
1506
- """
0 commit comments