Skip to content

Commit 4bc5df6

Browse files
Fix issue when converting matplotlib lines+markers legend to plotly
1 parent 1ec864b commit 4bc5df6

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
@@ -371,6 +371,12 @@ def draw_legend_shapes(self, mode, shape, **props):
371371
x1 = props["data"][1][0]
372372
y1 = props["data"][1][1]
373373

374+
lines_shape = shape.copy()
375+
ignored_props = ["symbol", "size"]
376+
for prop in ignored_props:
377+
if prop in lines_shape:
378+
lines_shape.pop(prop)
379+
374380
legend_shape = go.layout.Shape(
375381
type=mode,
376382
xref="paper",
@@ -379,7 +385,7 @@ def draw_legend_shapes(self, mode, shape, **props):
379385
y0=y + 0.02,
380386
x1=x1,
381387
y1=y1 + 0.02,
382-
**shape,
388+
**lines_shape,
383389
)
384390
else:
385391
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)