diff --git a/plotly/matplotlylib/mpltools.py b/plotly/matplotlylib/mpltools.py index ad01b37520..83fd92bcd9 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 65bbcfabb1..4c0d7e8b81 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -407,6 +407,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 18ce2d02b3..14cf365af2 100644 --- a/plotly/matplotlylib/tests/test_renderer.py +++ b/plotly/matplotlylib/tests/test_renderer.py @@ -37,6 +37,22 @@ def test_no_fake_legend_shapes_with_native_legend(): assert len(plotly_fig.layout.annotations) == 0 +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 + + def test_legend_disabled_when_no_matplotlib_legend(): """Test that legend is not enabled when no matplotlib legend is present.""" fig, ax = plt.subplots()