Skip to content

Commit 6ffb480

Browse files
committed
Fix flaky labelcolor tests
For labelcolor={linecolor,markeredgecolor,markerfacecolor}, text will match the specified attribute if consistent, but fall back to black if they differ within a single labeled artist. These tests use 10 random colours out of the ['r', 'g', 'b'] set, so 3 (all red, all green, all blue) out of 3**10 will result in the text _not_ being black. This is rare (0.0051%), but does happen once in a while. Instead, just hard-code some different colours in the test.
1 parent fe6389f commit 6ffb480

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/matplotlib/tests/test_legend.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,8 @@ def test_legend_pathcollection_labelcolor_linecolor_iterable():
868868
# test the labelcolor for labelcolor='linecolor' on PathCollection
869869
# with iterable colors
870870
fig, ax = plt.subplots()
871-
colors = np.random.default_rng().choice(['r', 'g', 'b'], 10)
872-
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', c=colors)
871+
colors = np.array(['r', 'g', 'b', 'c', 'm'] * 2)
872+
ax.scatter(np.arange(10), np.arange(10), label='#1', c=colors)
873873

874874
leg = ax.legend(labelcolor='linecolor')
875875
text, = leg.get_texts()
@@ -915,8 +915,8 @@ def test_legend_pathcollection_labelcolor_markeredgecolor_iterable():
915915
# test the labelcolor for labelcolor='markeredgecolor' on PathCollection
916916
# with iterable colors
917917
fig, ax = plt.subplots()
918-
colors = np.random.default_rng().choice(['r', 'g', 'b'], 10)
919-
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', edgecolor=colors)
918+
colors = np.array(['r', 'g', 'b', 'c', 'm'] * 2)
919+
ax.scatter(np.arange(10), np.arange(10), label='#1', edgecolor=colors)
920920

921921
leg = ax.legend(labelcolor='markeredgecolor')
922922
for text, color in zip(leg.get_texts(), ['k']):
@@ -970,8 +970,8 @@ def test_legend_pathcollection_labelcolor_markerfacecolor_iterable():
970970
# test the labelcolor for labelcolor='markerfacecolor' on PathCollection
971971
# with iterable colors
972972
fig, ax = plt.subplots()
973-
colors = np.random.default_rng().choice(['r', 'g', 'b'], 10)
974-
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', facecolor=colors)
973+
colors = np.array(['r', 'g', 'b', 'c', 'm'] * 2)
974+
ax.scatter(np.arange(10), np.arange(10), label='#1', facecolor=colors)
975975

976976
leg = ax.legend(labelcolor='markerfacecolor')
977977
for text, color in zip(leg.get_texts(), ['k']):

0 commit comments

Comments
 (0)