diff --git a/plotly/matplotlylib/renderer.py b/plotly/matplotlylib/renderer.py index e05a046607a..cdda2aaa94f 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -199,7 +199,6 @@ def close_axes(self, ax): self.x_is_mpl_date = False def draw_bars(self, bars): - # sort bars according to bar containers mpl_traces = [] for container in self.bar_containers: @@ -300,7 +299,7 @@ def draw_bar(self, coll): ) # TODO ditto if len(bar["x"]) > 1: self.msg += " Heck yeah, I drew that bar chart\n" - self.plotly_fig.add_trace(bar), + self.plotly_fig.add_trace(bar) if bar_gap is not None: self.plotly_fig["layout"]["bargap"] = bar_gap else: @@ -371,6 +370,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", @@ -379,7 +384,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" @@ -498,13 +503,13 @@ def draw_marked_line(self, **props): marked_line["x"] = mpltools.mpl_dates_to_datestrings( marked_line["x"], formatter ) - self.plotly_fig.add_trace(marked_line), + self.plotly_fig.add_trace(marked_line) self.msg += " Heck yeah, I drew that line\n" elif props["coordinates"] == "axes": # dealing with legend graphical elements self.draw_legend_shapes(mode=mode, shape=shape, **props) else: - self.msg += " Line didn't have 'data' coordinates, " "not drawing\n" + self.msg += " Line didn't have 'data' coordinates, not drawing\n" warnings.warn( "Bummer! Plotly can currently only draw Line2D " "objects from matplotlib that are in 'data' " @@ -568,7 +573,7 @@ def draw_path_collection(self, **props): self.msg += " Drawing path collection as markers\n" self.draw_marked_line(**scatter_props) else: - self.msg += " Path collection not linked to 'data', " "not drawing\n" + self.msg += " Path collection not linked to 'data', not drawing\n" warnings.warn( "Dang! That path collection is out of this " "world. I totally don't know what to do with " @@ -669,9 +674,7 @@ def draw_text(self, **props): else: # just a regular text annotation... self.msg += " Text object is a normal annotation\n" if props["coordinates"] != "data": - self.msg += ( - " Text object isn't linked to 'data' " "coordinates\n" - ) + self.msg += " Text object isn't linked to 'data' coordinates\n" x_px, y_px = ( props["mplobj"].get_transform().transform(props["position"]) ) @@ -681,7 +684,7 @@ def draw_text(self, **props): xanchor = props["style"]["halign"] # no difference here! yanchor = mpltools.convert_va(props["style"]["valign"]) else: - self.msg += " Text object is linked to 'data' " "coordinates\n" + self.msg += " Text object is linked to 'data' coordinates\n" x, y = props["position"] axis_ct = self.axis_ct xaxis = self.plotly_fig["layout"]["xaxis{0}".format(axis_ct)] @@ -757,9 +760,7 @@ def draw_title(self, **props): """ self.msg += " Attempting to draw a title\n" if len(self.mpl_fig.axes) > 1: - self.msg += ( - " More than one subplot, adding title as " "annotation\n" - ) + self.msg += " More than one subplot, adding title as annotation\n" x_px, y_px = props["mplobj"].get_transform().transform(props["position"]) x, y = mpltools.display_to_paper(x_px, y_px, self.plotly_fig["layout"]) annotation = go.layout.Annotation( @@ -777,9 +778,7 @@ def draw_title(self, **props): ) self.plotly_fig["layout"]["annotations"] += (annotation,) else: - self.msg += ( - " Only one subplot found, adding as a " "plotly title\n" - ) + self.msg += " Only one subplot found, adding as a plotly title\n" self.plotly_fig["layout"]["title"] = props["text"] title_font = dict( size=props["style"]["fontsize"], color=props["style"]["color"] diff --git a/plotly/matplotlylib/tests/__init__.py b/plotly/matplotlylib/tests/__init__.py new file mode 100644 index 00000000000..c29e9896d61 --- /dev/null +++ b/plotly/matplotlylib/tests/__init__.py @@ -0,0 +1,4 @@ +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt diff --git a/plotly/matplotlylib/tests/test_renderer.py b/plotly/matplotlylib/tests/test_renderer.py new file mode 100644 index 00000000000..019e96da390 --- /dev/null +++ b/plotly/matplotlylib/tests/test_renderer.py @@ -0,0 +1,19 @@ +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"