Skip to content

Fix conversion of matplotlib contour lines - #5770

Open
robertoffmoura wants to merge 5 commits into
plotly:mainfrom
robertoffmoura:rm/fix-contour-plot
Open

robertoffmoura wants to merge 5 commits into
plotly:mainfrom
robertoffmoura:rm/fix-contour-plot

Conversation

@robertoffmoura

Copy link
Copy Markdown
Contributor

mpl_to_plotly doesn't render contour lines correctly. Matplotlib packs each contour level into a single path containing several disjoint subpaths, so the converted figure connects them with segments that shouldn't exist, and closed rings are left open because the CLOSEPOLY vertex is dropped on export:

  • separate branches of the same level get joined by diagonals
  • every closed loop misses its final segment

Fix: path collections with no face colors (line collections, which is what ax.contour produces) are now drawn as line traces instead of filled polygons:

  • disjoint subpaths inside a path are separated with None, so plotly does not connect them
  • subpaths ending in a Z code have their first vertex appended to close the ring
  • multi-vertex path codes (C, S) consume the correct number of vertices while parsing
  • degenerate paths (fewer than 2 vertices, e.g. the empty path some contour sets carry) are skipped
  • x-values are converted to date strings on date axes, and per-path edge colors/linewidths are applied in order

Snippet to reproduce:

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

x = np.linspace(-2, 2, 120)
X, Y = np.meshgrid(x, x)
Z = np.sin(3 * np.sqrt(X**2 + Y**2))

fig, ax = plt.subplots()
ax.contour(X, Y, Z, levels=[-0.75, -0.25, 0.25, 0.75], linewidths=2)
fig.savefig("contour_mpl.png")

p = tls.mpl_to_plotly(fig)
p.write_image("contour_plotly.png")
matplotlib plotly before plotly after
contour_mpl contour_plotly_before contour_plotly_after

@camdecoster camdecoster self-assigned this Sep 23, 2026

This branch has not been deployed

No deployments
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