diff --git a/plotly/matplotlylib/mpltools.py b/plotly/matplotlylib/mpltools.py index 0a3206998b..b6fab3cb8d 100644 --- a/plotly/matplotlylib/mpltools.py +++ b/plotly/matplotlylib/mpltools.py @@ -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: diff --git a/plotly/matplotlylib/renderer.py b/plotly/matplotlylib/renderer.py index a282c67cec..00639483a2 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -404,6 +404,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( diff --git a/plotly/matplotlylib/tests/test_renderer.py b/plotly/matplotlylib/tests/test_renderer.py index f56d830917..cd9b78c619 100644 --- a/plotly/matplotlylib/tests/test_renderer.py +++ b/plotly/matplotlylib/tests/test_renderer.py @@ -199,3 +199,17 @@ 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_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