Skip to content

Commit 2a42707

Browse files
Fix issue when converting matplotlib lines+markers legend to plotly
1 parent 694b036 commit 2a42707

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

plotly/matplotlylib/renderer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ def draw_legend_shapes(self, mode, shape, **props):
370370
x1 = props["data"][1][0]
371371
y1 = props["data"][1][1]
372372

373+
lines_shape = shape.copy()
374+
ignored_props = ["symbol", "size"]
375+
for prop in ignored_props:
376+
if prop in lines_shape:
377+
lines_shape.pop(prop)
378+
373379
legend_shape = go.layout.Shape(
374380
type=mode,
375381
xref="paper",
@@ -378,7 +384,7 @@ def draw_legend_shapes(self, mode, shape, **props):
378384
y0=y + 0.02,
379385
x1=x1,
380386
y1=y1 + 0.02,
381-
**shape,
387+
**lines_shape,
382388
)
383389
else:
384390
self.msg += "not sure how to handle this element\n"

plotly/matplotlylib/tests/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import matplotlib
2+
3+
matplotlib.use("Agg")
4+
import matplotlib.pyplot as plt
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import plotly.tools as tls
2+
3+
from . import plt
4+
5+
def test_lines_markers_legend_plot():
6+
x = [0, 1]
7+
y = [0, 1]
8+
label = "label"
9+
plt.figure()
10+
plt.plot(x, y, "o-", label=label)
11+
plt.legend()
12+
13+
plotly_fig = tls.mpl_to_plotly(plt.gcf())
14+
15+
assert plotly_fig.data[0].mode == "lines+markers"
16+
assert plotly_fig.data[0].x == tuple(x)
17+
assert plotly_fig.data[0].y == tuple(y)
18+
assert plotly_fig.data[0].name == "label"

0 commit comments

Comments
 (0)