Skip to content
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
17 changes: 17 additions & 0 deletions plotly/matplotlylib/mpltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,23 @@ def get_bar_gap(bar_starts, bar_ends, tol=1e-10):
return gap0


DRAWSTYLE_SHAPE_MAP = {
"steps": "vh",
"steps-pre": "vh",
"steps-post": "hv",
"steps-mid": "hvh",
}


def convert_drawstyle(drawstyle):
"""Convert a matplotlib line drawstyle to a plotly line shape.

Matplotlib draws steps as vertical/horizontal segments; plotly's
``line.shape`` expresses the same via "vh", "hv" and "hvh".
"""
return DRAWSTYLE_SHAPE_MAP.get(drawstyle)


def convert_rgba_array(color_list):
clean_color_list = list()
for c in color_list:
Expand Down
3 changes: 3 additions & 0 deletions plotly/matplotlylib/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ def draw_marked_line(self, **props):
color=color,
width=props["linestyle"]["linewidth"],
dash=mpltools.convert_dash(props["linestyle"]["dasharray"]),
shape=mpltools.convert_drawstyle(
props["linestyle"]["drawstyle"]
),
)
else:
shape = dict(
Expand Down
14 changes: 14 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,17 @@ def test_multiple_traces_native_legend():
assert plotly_fig.data[0].mode == "lines"
assert plotly_fig.data[1].mode == "markers"
assert plotly_fig.data[2].mode == "lines+markers"


def test_drawstyle_maps_to_line_shape():
cases = {
"steps-pre": "vh",
"steps": "vh",
"steps-post": "hv",
"steps-mid": "hvh",
}
for drawstyle, shape in cases.items():
fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 0], drawstyle=drawstyle)
plotly_fig = tls.mpl_to_plotly(fig)
assert plotly_fig.data[0].line.shape == shape