Skip to content

Fix issue when converting matplotlib lines+markers legend to plotly #5232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion plotly/matplotlylib/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ def draw_legend_shapes(self, mode, shape, **props):
x1 = props["data"][1][0]
y1 = props["data"][1][1]

lines_shape = shape.copy()
ignored_props = ["symbol", "size"]
for prop in ignored_props:
if prop in lines_shape:
lines_shape.pop(prop)

legend_shape = go.layout.Shape(
type=mode,
xref="paper",
Expand All @@ -379,7 +385,7 @@ def draw_legend_shapes(self, mode, shape, **props):
y0=y + 0.02,
x1=x1,
y1=y1 + 0.02,
**shape,
**lines_shape,
)
else:
self.msg += "not sure how to handle this element\n"
Expand Down
4 changes: 4 additions & 0 deletions plotly/matplotlylib/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
18 changes: 18 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import plotly.tools as tls

from . import plt

def test_lines_markers_legend_plot():
x = [0, 1]
y = [0, 1]
label = "label"
plt.figure()
plt.plot(x, y, "o-", label=label)
plt.legend()

plotly_fig = tls.mpl_to_plotly(plt.gcf())

assert plotly_fig.data[0].mode == "lines+markers"
assert plotly_fig.data[0].x == tuple(x)
assert plotly_fig.data[0].y == tuple(y)
assert plotly_fig.data[0].name == "label"