Skip to content

Convert matplotlib step drawstyles to plotly line shapes - #5697

Merged
camdecoster merged 4 commits into
plotly:mainfrom
robertoffmoura:rm/fix-step-plot
Oct 7, 2026
Merged

camdecoster merged 4 commits into
plotly:mainfrom
robertoffmoura:rm/fix-step-plot

Conversation

@robertoffmoura

@robertoffmoura robertoffmoura commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

mpl_to_plotly converts plt.step plots (and any drawstyle="steps-*" line) as ordinary straight lines, connecting the points with diagonals instead of the vertical/horizontal step segments matplotlib draws.

Before: the converted trace has line.shape = None, so plotly draws diagonal connections between consecutive points.

After: the drawstyle is mapped to plotly's step shapes, matching matplotlib's geometry exactly:

matplotlib drawstyle plotly line.shape
steps-pre (and plt.step, steps) vh
steps-post hv
steps-mid hvh

Steps to reproduce:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import plotly.tools as tls

x = np.linspace(0, 10, 20)
y = np.sin(x)

fig, ax = plt.subplots()
ax.step(x, y)
fig.savefig("step_mpl.png")

p = tls.mpl_to_plotly(fig)
p.write_image("step_plotly.png")

print(p.data[0].line.shape)   # "vh" with the fix; None before
matplotlib plotly before plotly after
step_mpl step_plotly_before step_plotly_after

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Could you please add a changelog entry? Also, I've noticed that these tests don't run in CI. Do you know if that's intentional? It's not from a recent change that you made, but I'm curious if you know the history.

@robertoffmoura

Copy link
Copy Markdown
Contributor Author

Thanks! I've added the changelog entry.

Regarding the history: It looks like the original tests (in tests/test_optional/test_matplotlylib/) were intentionally skipped in 2022 (b7ba00b) because they were failing.

When I added the test_renderer suite (plotly/matplotlylib/tests/test_renderer.py) (#5312), I unintentionally never wired it into run-pytest.yml.

I added a dedicated test-matplotlylib job to run-pytest.yml that installs matplotlib and runs the tests across all supported Python versions (3.8–3.13). Happy to keep it in this PR, or spin it off into a separate PR if you prefer to keep this one focused purely on step plots. Let me know what you think!

@robertoffmoura
robertoffmoura force-pushed the rm/fix-step-plot branch 2 times, most recently from bf290b2 to fff1d2a Compare October 4, 2026 02:51
@camdecoster

Copy link
Copy Markdown
Contributor

Thanks for looking into it! Could you please move the CI update to a separate PR? Once that's done, I'll approve this PR.

@robertoffmoura

Copy link
Copy Markdown
Contributor Author

Thanks! I rebased and moved the CI update to #5794

@camdecoster
camdecoster merged commit 52d0c7f into plotly:main Oct 7, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants