Skip to content

Commit b25cbdd

Browse files
Fix boxplot conversion by mapping 'none' colors to transparent rgba
1 parent 73f86fd commit b25cbdd

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
@@ -326,7 +326,7 @@ def draw_bar(self, coll):
326326
yaxis="y{0}".format(self.axis_ct),
327327
opacity=trace[0]["alpha"], # TODO: get all alphas if array?
328328
marker=go.bar.Marker(
329-
color=trace[0]["facecolor"], # TODO: get all
329+
color=_export_color(trace[0]["facecolor"]), # TODO: get all
330330
line=dict(width=trace[0]["edgewidth"]),
331331
),
332332
) # TODO ditto
@@ -388,9 +388,13 @@ def draw_marked_line(self, **props):
388388
self.msg += "... with just markers\n"
389389
mode = "markers"
390390
if props["linestyle"]:
391-
color = mpltools.merge_color_and_opacity(
392-
props["linestyle"]["color"], props["linestyle"]["alpha"]
393-
)
391+
if props["linestyle"]["color"] == "none":
392+
# a fully transparent line; plotly rejects "none" as a color
393+
color = "rgba(0,0,0,0)"
394+
else:
395+
color = mpltools.merge_color_and_opacity(
396+
props["linestyle"]["color"], props["linestyle"]["alpha"]
397+
)
394398

395399
if props["coordinates"] == "data":
396400
line = go.scatter.Line(
@@ -410,22 +414,22 @@ def draw_marked_line(self, **props):
410414
if props["coordinates"] == "data":
411415
marker = go.scatter.Marker(
412416
opacity=props["markerstyle"]["alpha"],
413-
color=props["markerstyle"]["facecolor"],
417+
color=_export_color(props["markerstyle"]["facecolor"]),
414418
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
415419
size=props["markerstyle"]["markersize"],
416420
line=dict(
417-
color=props["markerstyle"]["edgecolor"],
421+
color=_export_color(props["markerstyle"]["edgecolor"]),
418422
width=props["markerstyle"]["edgewidth"],
419423
),
420424
)
421425
else:
422426
shape = dict(
423427
opacity=props["markerstyle"]["alpha"],
424-
fillcolor=props["markerstyle"]["facecolor"],
428+
fillcolor=_export_color(props["markerstyle"]["facecolor"]),
425429
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
426430
size=props["markerstyle"]["markersize"],
427431
line=dict(
428-
color=props["markerstyle"]["edgecolor"],
432+
color=_export_color(props["markerstyle"]["edgecolor"]),
429433
width=props["markerstyle"]["edgewidth"],
430434
),
431435
)

‎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)