Skip to content
Merged
Prev Previous commit
Next Next commit
direct comparison to source colors, face!=line
  • Loading branch information
landoskape committed Jan 29, 2025
commit f219c3f935d3b261c9011c65be031b881303a5f6
32 changes: 12 additions & 20 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4065,45 +4065,37 @@ def test_violinplot_color_sequence():
# each color independently
np.random.seed(19680801)
data = [sorted(np.random.normal(0, std, 100)) for std in range(1, 5)]
kwargs = {'showmeans': True,
'showextrema': True,
'showmedians': True
}
kwargs = {'showmeans': True, 'showextrema': True, 'showmedians': True}

def assert_colors_equal(colors1, colors2):
assert all(mcolors.same_color(c1, c2) for c1, c2 in zip(colors1, colors2))
assert all(mcolors.same_color(c1, c2)
for c1, c2 in zip(colors1, colors2))

parts_with_facecolor = ["bodies"]
parts_with_edgecolor = ["cbars", "cmins", "cmaxes", "cmeans", "cmedians"]

# Color sequence
N = len(data)
positions = range(N)
colors = ['k', 'r', ('b', 0.5), 'g', ('m', 0.2)]
facecolors = ['k', 'r', ('b', 0.5), ('g', 0.2)]
linecolors = [('y', 0.4), 'b', 'm', ('k', 0.8)]

# Test image
fig_test = plt.figure()
ax = fig_test.gca()
parts_test = ax.violinplot(data, positions=positions, facecolor=colors,
linecolor=colors, **kwargs)

# Reference image
fig_ref = plt.figure()
ax = fig_ref.gca()
parts_ref = []
for (p, c, d) in zip(positions, colors, data):
cparts = ax.violinplot(d, positions=[p], facecolor=c, linecolor=c, **kwargs)
parts_ref.append(cparts)
parts_test = ax.violinplot(data,
positions=positions,
facecolor=facecolors,
linecolor=linecolors,
**kwargs)

for part in parts_with_facecolor:
colors_test = [p.get_facecolor() for p in parts_test[part]]
colors_ref = [p[part][0].get_facecolor() for p in parts_ref]
assert_colors_equal(colors_test, colors_ref)
assert_colors_equal(colors_test, mcolors.to_rgba_array(facecolors))

for part in parts_with_edgecolor:
Comment thread
timhoffm marked this conversation as resolved.
Outdated
colors_test = parts_test[part].get_edgecolor()
colors_ref = [p[part].get_edgecolor() for p in parts_ref]
assert_colors_equal(colors_test, colors_ref)
assert_colors_equal(colors_test, mcolors.to_rgba_array(linecolors))


@check_figures_equal(extensions=["png"])
Expand Down