Skip to content

Fix PDF path collection culling for hexbin offsets#32000

Open
yuzie007 wants to merge 2 commits into
matplotlib:mainfrom
yuzie007:fix-pdf-draw-path-collection
Open

Fix PDF path collection culling for hexbin offsets#32000
yuzie007 wants to merge 2 commits into
matplotlib:mainfrom
yuzie007:fix-pdf-draw-path-collection

Conversation

@yuzie007

@yuzie007 yuzie007 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

PR summary

This PR fixes a PDF backend regression where visible hexbin cells can be incorrectly skipped in Matplotlib 3.11.0.

Closes #31999.

PR #30746 added path collection culling to the PDF backend to avoid bloated PDFs when many colored scatter markers are fully outside the canvas. That optimization works for scatter, where transformed offsets correspond to marker centers in canvas coordinates. However, hexbin uses a repeated hexagon path with offsets transformed by AffineDeltaTransform(self.transData). Those transformed offsets are displacements, not absolute canvas-space marker
centers.

The PDF backend was culling collections by checking the transformed offset plus an approximate marker extent. For hexbin, this can classify visible cells as off-canvas, especially when offsets are negative, so the PDF output loses part of the plot while raster output renders correctly.

This PR keeps the optimization from #30746, but changes the culling check to use the actual transformed reusable path bounds translated by each offset. This preserves PDF size improvements for truly off-canvas scatter markers while correctly rendering hexbin and other collections whose transformed offsets are not absolute marker centers. This fix make the example in #31999 work as expected.

A PDF regression test for hexbin with negative offsets, compared against an equivalent shifted reference.

AI Disclosure

Agentic AI was employed to identify the origin of the issue, make the original fix, add the original test, and write the original issue and the PR description. The AI suggestions were reviewed and polished by human.

PR checklist

@yuzie007 yuzie007 force-pushed the fix-pdf-draw-path-collection branch from 1cec52b to 3e5af6f Compare July 5, 2026 10:52
@QuLogic QuLogic added this to the v3.11.1 milestone Jul 9, 2026
# Optimization: Fast path for markers with centers inside canvas.
# This avoids the dictionary lookup for the common case where
# markers are visible, improving performance for large scatter plots.
if 0 <= xo <= canvas_width and 0 <= yo <= canvas_height:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it possible to look at offset_trans condition the fast path on that as well (my suspicion is that this is Identity by default and that is the only safe case).

Alternatively, would it make sense to apply the offset_trans when doing the check?

facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position, hatchcolors=hatchcolors):

# Optimization: Fast path for markers with centers inside canvas.

@tacaswell tacaswell Jul 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we have any benchmarks on what we are giving up performance-wise here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: PDF backend culls visible hexbin cells after path collection optimization

3 participants