Skip to content

Commit 6e40185

Browse files
Fix boxplot conversion by mapping 'none' colors to transparent rgba
1 parent a5974ce commit 6e40185

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

plotly/matplotlylib/renderer.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def draw_bar(self, coll):
333333
yaxis="y{0}".format(self.axis_ct),
334334
opacity=trace[0]["alpha"], # TODO: get all alphas if array?
335335
marker=go.bar.Marker(
336-
color=trace[0]["facecolor"], # TODO: get all
336+
color=_export_color(trace[0]["facecolor"]), # TODO: get all
337337
line=dict(width=trace[0]["edgewidth"]),
338338
),
339339
) # TODO ditto
@@ -395,9 +395,13 @@ def draw_marked_line(self, **props):
395395
self.msg += "... with just markers\n"
396396
mode = "markers"
397397
if props["linestyle"]:
398-
color = mpltools.merge_color_and_opacity(
399-
props["linestyle"]["color"], props["linestyle"]["alpha"]
400-
)
398+
if props["linestyle"]["color"] == "none":
399+
# a fully transparent line; plotly rejects "none" as a color
400+
color = "rgba(0,0,0,0)"
401+
else:
402+
color = mpltools.merge_color_and_opacity(
403+
props["linestyle"]["color"], props["linestyle"]["alpha"]
404+
)
401405

402406
if props["coordinates"] == "data":
403407
line = go.scatter.Line(
@@ -417,22 +421,22 @@ def draw_marked_line(self, **props):
417421
if props["coordinates"] == "data":
418422
marker = go.scatter.Marker(
419423
opacity=props["markerstyle"]["alpha"],
420-
color=props["markerstyle"]["facecolor"],
424+
color=_export_color(props["markerstyle"]["facecolor"]),
421425
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
422426
size=props["markerstyle"]["markersize"],
423427
line=dict(
424-
color=props["markerstyle"]["edgecolor"],
428+
color=_export_color(props["markerstyle"]["edgecolor"]),
425429
width=props["markerstyle"]["edgewidth"],
426430
),
427431
)
428432
else:
429433
shape = dict(
430434
opacity=props["markerstyle"]["alpha"],
431-
fillcolor=props["markerstyle"]["facecolor"],
435+
fillcolor=_export_color(props["markerstyle"]["facecolor"]),
432436
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
433437
size=props["markerstyle"]["markersize"],
434438
line=dict(
435-
color=props["markerstyle"]["edgecolor"],
439+
color=_export_color(props["markerstyle"]["edgecolor"]),
436440
width=props["markerstyle"]["edgewidth"],
437441
),
438442
)

plotly/matplotlylib/tests/test_renderer.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,21 @@ def test_filled_path_collection_date_xaxis():
199199
filled = [t for t in plotly_fig.data if t.fill == "toself"]
200200
assert len(filled) >= 1
201201
assert all(isinstance(x, str) for x in filled[0].x)
202+
203+
204+
def test_boxplot_converts_with_none_marker_facecolor():
205+
"""Boxplot outlier markers use facecolor 'none', which plotly rejects."""
206+
fig, ax = plt.subplots()
207+
ax.boxplot(np.random.randn(100, 4))
208+
plotly_fig = tls.mpl_to_plotly(fig) # used to raise ValueError
209+
assert len(plotly_fig.data) > 0
210+
211+
212+
def test_line_with_none_color_converts():
213+
"""Lines with color='none' use the string 'none' for the line color,
214+
which plotly rejects; it must be exported as a transparent line."""
215+
fig, ax = plt.subplots()
216+
ax.plot([0, 1], [0, 1], color="none")
217+
plotly_fig = tls.mpl_to_plotly(fig) # used to raise ValueError
218+
assert len(plotly_fig.data) == 1
219+
assert plotly_fig.data[0].line.color == "rgba(0,0,0,0)"

0 commit comments

Comments
 (0)