Skip to content

Commit df0db87

Browse files
Fix boxplot conversion by mapping 'none' colors to transparent rgba
1 parent d3105d4 commit df0db87

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

plotly/matplotlylib/renderer.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def draw_bar(self, coll):
336336
yaxis="y{0}".format(self.axis_ct),
337337
opacity=trace[0]["alpha"], # TODO: get all alphas if array?
338338
marker=go.bar.Marker(
339-
color=trace[0]["facecolor"], # TODO: get all
339+
color=_export_color(trace[0]["facecolor"]), # TODO: get all
340340
line=dict(width=trace[0]["edgewidth"]),
341341
),
342342
) # TODO ditto
@@ -398,9 +398,13 @@ def draw_marked_line(self, **props):
398398
self.msg += "... with just markers\n"
399399
mode = "markers"
400400
if props["linestyle"]:
401-
color = mpltools.merge_color_and_opacity(
402-
props["linestyle"]["color"], props["linestyle"]["alpha"]
403-
)
401+
if props["linestyle"]["color"] == "none":
402+
# a fully transparent line; plotly rejects "none" as a color
403+
color = "rgba(0,0,0,0)"
404+
else:
405+
color = mpltools.merge_color_and_opacity(
406+
props["linestyle"]["color"], props["linestyle"]["alpha"]
407+
)
404408

405409
if props["coordinates"] == "data":
406410
line = go.scatter.Line(
@@ -420,22 +424,22 @@ def draw_marked_line(self, **props):
420424
if props["coordinates"] == "data":
421425
marker = go.scatter.Marker(
422426
opacity=props["markerstyle"]["alpha"],
423-
color=props["markerstyle"]["facecolor"],
427+
color=_export_color(props["markerstyle"]["facecolor"]),
424428
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
425429
size=props["markerstyle"]["markersize"],
426430
line=dict(
427-
color=props["markerstyle"]["edgecolor"],
431+
color=_export_color(props["markerstyle"]["edgecolor"]),
428432
width=props["markerstyle"]["edgewidth"],
429433
),
430434
)
431435
else:
432436
shape = dict(
433437
opacity=props["markerstyle"]["alpha"],
434-
fillcolor=props["markerstyle"]["facecolor"],
438+
fillcolor=_export_color(props["markerstyle"]["facecolor"]),
435439
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
436440
size=props["markerstyle"]["markersize"],
437441
line=dict(
438-
color=props["markerstyle"]["edgecolor"],
442+
color=_export_color(props["markerstyle"]["edgecolor"]),
439443
width=props["markerstyle"]["edgewidth"],
440444
),
441445
)

plotly/matplotlylib/tests/test_renderer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ def test_pcolor_rectangles_render():
107107
assert all(len(t.x) >= 4 for t in plotly_fig.data)
108108

109109

110+
def test_boxplot_converts_with_none_marker_facecolor():
111+
"""Boxplot outlier markers use facecolor 'none', which plotly rejects."""
112+
fig, ax = plt.subplots()
113+
ax.boxplot(np.random.randn(100, 4))
114+
115+
plotly_fig = tls.mpl_to_plotly(fig)
116+
117+
assert len(plotly_fig.data) > 0
118+
119+
120+
def test_line_with_none_color_converts():
121+
"""Lines with color='none' use the string 'none' for the line color,
122+
which plotly rejects; it must be exported as a transparent line."""
123+
fig, ax = plt.subplots()
124+
ax.plot([0, 1], [0, 1], color="none")
125+
126+
plotly_fig = tls.mpl_to_plotly(fig)
127+
128+
assert len(plotly_fig.data) == 1
129+
assert plotly_fig.data[0].line.color == "rgba(0,0,0,0)"
130+
131+
110132
def test_eventplot_segments_render():
111133
fig, ax = plt.subplots()
112134
ax.eventplot([np.random.randn(20) for _ in range(5)])

0 commit comments

Comments
 (0)