FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Convert matplotlib stairs plots to plotly step lines by robertoffmoura · Pull Request #5698 · plotly/plotly.py · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
36 changes: 36 additions & 0 deletions plotly/matplotlylib/renderer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import warnings

import matplotlib.patches as mpatches
import plotly.graph_objs as go
from plotly.matplotlylib.mplexporter import Renderer
from plotly.matplotlylib import mpltools
Expand Down Expand Up @@ -599,13 +600,48 @@ def draw_path(self, **props):
is_bar = mpltools.is_bar(self.current_mpl_ax.containers, **props)
if is_bar:
self.current_bars += [props]
elif isinstance(props["mplobj"], mpatches.StepPatch):
self.msg += " Drawing a step path\n"
self._draw_step_path(props)
else:
self.msg += " This path isn't a bar, not drawing\n"
warnings.warn(
"I found a path object that I don't think is part "
"of a bar chart. Ignoring."
)

def _draw_step_path(self, props):
"""Draw a matplotlib StepPatch as a step line trace."""
if props["coordinates"] != "data":
self.msg += " Step path is not in data coordinates, not drawing\n"
return
style = props["style"]
x = []
y = []
for x0, y0 in props["data"]:
if not x or x0 != x[-1] or y0 != y[-1]:
x.append(x0)
y.append(y0)
if len(x) < 2:
self.msg += " Step path has fewer than 2 points, not drawing\n"
return
self.plotly_fig.add_trace(
go.Scatter(
x=x,
y=y,
mode="lines",
line=go.scatter.Line(
color=mpltools.merge_color_and_opacity(
style["edgecolor"], style["alpha"]
),
width=style["edgewidth"],
dash=mpltools.convert_dash(style["dasharray"]),
),
xaxis="x{0}".format(self.axis_ct),
yaxis="y{0}".format(self.axis_ct),
)
)

def draw_text(self, **props):
"""Create an annotation dict for a text obj.

Expand Down
11 changes: 11 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,14 @@ def test_filled_path_collection_date_xaxis():
filled = [t for t in plotly_fig.data if t.fill == "toself"]
assert len(filled) >= 1
assert all(isinstance(x, str) for x in filled[0].x)


def test_stairs_converts_to_step_line():
fig, ax = plt.subplots()
ax.stairs([0.0, 1.0, 0.0], [0.0, 1.0, 2.0, 3.0])
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) == 1
trace = plotly_fig.data[0]
assert trace.mode == "lines"
assert tuple(trace.x) == (0.0, 1.0, 1.0, 2.0, 2.0, 3.0)
assert tuple(trace.y) == (0.0, 0.0, 1.0, 1.0, 0.0, 0.0)

Back | FazBrowse Home | New Git URL