Skip to content

Convert matplotlib fill polygons to plotly filled scatter traces - #5

Open
robertoffmoura wants to merge 17 commits into
mainfrom
rm/support-fill-plots
Open

robertoffmoura wants to merge 17 commits into
mainfrom
rm/support-fill-plots

Conversation

@robertoffmoura

@robertoffmoura robertoffmoura commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Originally opened as plotly/plotly.py#5795.

mpl_to_plotly drops plt.fill polygons entirely. Matplotlib's fill creates Polygon artists, which the exporter treats as generic paths, and the renderer only handles bar-chart paths, so each polygon is skipped with the warning:

I found a path object that I don't think is part of a bar chart. Ignoring.

The converted figure ends up with no fill at all.

Fix: draw_path now recognizes Polygon artists in data coordinates and draws them as filled scatter traces:

Snippet to reproduce:

import matplotlib.pyplot as plt
import numpy as np
from matplotlylib import mpl_to_plotly
from matplotlib.patches import Circle, Ellipse, PathPatch, Rectangle, Wedge
from matplotlib.path import Path


def save_comparison(fig, name):
    fig.savefig(f"{name}_mpl.png", bbox_inches="tight")
    p = mpl_to_plotly(fig)
    p.write_image(f"{name}_plotly.png")


# 1. Simple filled curve with alpha
x = np.linspace(0, 2 * np.pi, 100)
fig1, ax1 = plt.subplots()
ax1.fill(x, np.sin(x), "b", alpha=0.3)
ax1.plot(x, np.sin(x), "b-")
save_comparison(fig1, "fill_sine")

# 2. Polygon with custom facecolor and edge
fig2, ax2 = plt.subplots()
ax2.fill([0, 1, 2, 1], [0, 2, 1, -1], color="forestgreen", edgecolor="black")
save_comparison(fig2, "fill_polygon")

# 3. Two overlapping fills
fig3, ax3 = plt.subplots()
ax3.fill(x, np.sin(x), "r", alpha=0.4)
ax3.fill(x, np.cos(x), "g", alpha=0.4)
save_comparison(fig3, "fill_overlap")

# 4. Circle, Ellipse, Wedge, Rectangle and PathPatch
path = Path(
    [(3.2, 0.2), (3.8, 0.2), (3.8, 0.8), (3.5, 0.6), (3.2, 0.2)],
    [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY],
)
fig4, ax4 = plt.subplots()
ax4.add_patch(Circle((0.5, 0.5), 0.3, color="tab:blue"))
ax4.add_patch(Ellipse((1.5, 0.5), 0.6, 0.3, angle=30, color="tab:orange"))
ax4.add_patch(Wedge((2.5, 0.5), 0.35, 30, 270, color="tab:green"))
ax4.add_patch(Rectangle((0.2, 1.1), 0.6, 0.5, color="tab:purple"))
ax4.add_patch(PathPatch(path, color="tab:red"))
ax4.set_xlim(0, 4)
ax4.set_ylim(0, 2)
ax4.set_aspect("equal")
save_comparison(fig4, "patch_shapes")
matplotlib plotly before plotly after
fill_overlap_mpl fill_overlap_plotly_before fill_overlap_plotly_after
fill_polygon_mpl fill_polygon_plotly_before fill_polygon_plotly_after
fill_sine_mpl fill_sine_plotly_before fill_sine_plotly_after
patch_shapes_mpl patch_shapes_plotly_before patch_shapes_plotly_after

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.

1 participant