Skip to content

Commit 90d2d45

Browse files
committed
Fixed some errors based on the reviews
1 parent c6bc57f commit 90d2d45

File tree

3 files changed

+86
-186
lines changed

3 files changed

+86
-186
lines changed

lib/matplotlib/legend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ def __init__(
624624
continue
625625

626626
text.set_color(rgba)
627-
break
627+
break
628628
except AttributeError:
629629
continue
630630
else:

lib/matplotlib/tests/test_legend.py

Lines changed: 85 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ def test_legend_pathcollection_labelcolor_linecolor():
858858

859859
leg = ax.legend(labelcolor='linecolor')
860860
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')
862862

863863

864864
def test_legend_pathcollection_labelcolor_linecolor_iterable():
@@ -1476,31 +1476,93 @@ def test_boxplot_legend_labels():
14761476
assert bp4['medians'][0].get_label() == 'box A'
14771477
assert all(x.get_label().startswith("_") for x in bp4['medians'][1:])
14781478

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)
14841479

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)
14891551

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()
14941554

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]
14991557

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+
)
15041567

15051568
plt.close(fig)
1506-
"""

test.py

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy