diff --git a/.github/workflows/run-pytest.yml b/.github/workflows/run-pytest.yml index 39e877bdb5..d1f70c08e3 100644 --- a/.github/workflows/run-pytest.yml +++ b/.github/workflows/run-pytest.yml @@ -164,3 +164,30 @@ jobs: run: | source .venv/bin/activate python -m pytest tests/test_optional/test_kaleido/test_kaleido_v0_error.py + + test-matplotlylib: + name: Matplotlylib tests (Python ${{ matrix.python-version }}) + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - name: Set up Python + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + with: + python-version: ${{ matrix.python-version }} + - name: Set up uv + uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0 + - name: Install dependencies + run: | + uv venv + source .venv/bin/activate + uv sync --extra dev_core + uv pip install matplotlib + python --version + - name: Test matplotlylib + run: | + source .venv/bin/activate + python -m pytest plotly/matplotlylib/tests plotly/matplotlylib/mplexporter/tests diff --git a/CHANGELOG.md b/CHANGELOG.md index 0583fec45c..1a71ef4e04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Fix concurrent first access to lazily initialized graph object properties, which could raise `ValueError("Invalid value")` [[#3441](https://github.com/plotly/plotly.py/issues/3441)], with thanks to @hb1915 for the contribution! - Fix `px.sunburst`, `px.treemap` and `px.icicle` listing sectors in a different order on every run when `path` is used with a Polars DataFrame; sectors now follow their order of first appearance for all dataframe backends [[#5765](https://github.com/plotly/plotly.py/issues/5765)], with thanks to @Irahan2 for the contribution! +- Fix `mpl_to_plotly` drawing step plots with diagonal lines instead of vertical/horizontal step segments by mapping matplotlib step drawstyles (`steps-pre`, `steps-post`, `steps-mid`) to plotly line shapes [[#5697](https://github.com/plotly/plotly.py/pull/5697)], with thanks to @robertoffmoura for the contribution! ## [7.1.0] - 2026-09-15 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..eacb98fc0b 100644 --- a/plotly/matplotlylib/renderer.py +++ b/plotly/matplotlylib/renderer.py @@ -407,6 +407,7 @@ 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()