Skip to content

Commit 497b22a

Browse files
Add tests for color export
1 parent b7cef4a commit 497b22a

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,51 @@ def test_transparent_text_colors_export():
169169
assert plotly_fig.layout.yaxis.title.font.color == "rgba(0,0,0,0)"
170170

171171

172+
def test_export_color_maps_colors():
173+
"""_export_color maps matplotlib color strings to plotly colors, keeping
174+
or overriding the alpha as requested."""
175+
from plotly.matplotlylib.mpltools import _export_color
176+
177+
expected_mappings = {
178+
(None, None): None,
179+
("none", None): "rgba(0,0,0,0)",
180+
("#FF0000", None): "#FF0000",
181+
("#FF0000", 1): "rgba(255, 0, 0, 1)",
182+
("#FF0000", 0.5): "rgba(255, 0, 0, 0.5)",
183+
("rgba(255, 0, 0, 0.2)", None): "rgba(255, 0, 0, 0.2)",
184+
("rgba(255, 0, 0, 0.2)", 1): "rgba(255, 0, 0, 0.2)",
185+
("rgba(255, 0, 0, 0.2)", 0.5): "rgba(255, 0, 0, 0.5)",
186+
("rgb(255, 0, 0)", None): "rgb(255, 0, 0)",
187+
("rgb(255, 0, 0)", 0.5): "rgba(255, 0, 0, 0.5)",
188+
((1.0, 0.0, 0.0, 0.5), None): "rgba(255, 0, 0, 0.5)",
189+
((0.0, 1.0, 0.0, 1.0), None): "#00FF00",
190+
}
191+
for (color, opacity), expected in expected_mappings.items():
192+
result = _export_color(color, opacity)
193+
assert result == expected, (
194+
f"Input {color!r} with opacity {opacity!r} produced {result!r}, "
195+
f"expected {expected!r}"
196+
)
197+
198+
assert _export_color(["#FF0000", "none"]) == ["#FF0000", "rgba(0,0,0,0)"]
199+
assert _export_color(["rgba(255, 0, 0, 0.2)"], 0.5) == ["rgba(255, 0, 0, 0.5)"]
200+
201+
202+
def test_scatter_with_multiple_colors_converts():
203+
"""Scatter markers with per-point colors export a list of marker colors."""
204+
fig, ax = plt.subplots()
205+
ax.scatter([0, 1, 2], [0, 1, 2], c=["red", "green", "blue"])
206+
207+
plotly_fig = tls.mpl_to_plotly(fig)
208+
209+
assert plotly_fig.data[0].mode == "markers"
210+
assert plotly_fig.data[0].marker.color == (
211+
"rgba(255,0,0,1.0)",
212+
"rgba(0,128,0,1.0)",
213+
"rgba(0,0,255,1.0)",
214+
)
215+
216+
172217
def test_eventplot_segments_render():
173218
fig, ax = plt.subplots()
174219
ax.eventplot([np.random.randn(20) for _ in range(5)])

0 commit comments

Comments
 (0)