Skip to content

Fix Poly3DCollection ragged padding overflow warnings - #32310

Open
ShamikOfficial wants to merge 1 commit into
matplotlib:mainfrom
ShamikOfficial:fix/poly3d-ragged-nan-padding
Open

ShamikOfficial wants to merge 1 commit into
matplotlib:mainfrom
ShamikOfficial:fix/poly3d-ragged-nan-padding

Conversation

@ShamikOfficial

Copy link
Copy Markdown
Contributor

PR summary

When Poly3DCollection gets polygons with different vertex counts, _get_vector packs them into a rectangular array with np.empty(). Unused padding is marked in _invalid_vertices, but do_3d_projection still projects the whole array first. Uninitialized padding can hold huge float values and trigger nondeterministic RuntimeWarning: overflow encountered in dot.

This change initializes that padded storage with np.nan instead. NaN values do not overflow in the projection math, and do_3d_projection already ignores invalid floating point results. A regression test builds a square plus triangle collection, checks that padding is NaN, and draws under np.errstate(over="raise").

Closes #32272

Note: #32279 proposed the same fix earlier and was closed because of the automated-contribution account policy, not because the change itself was wrong.

AI Disclosure

I used Cursor to help locate the np.empty padding path, draft the regression test, and prepare the PR text. I reviewed the code path in Poly3DCollection._get_vector / do_3d_projection, confirmed why #32279 was closed, and verified the overflow reproduction plus the NaN-padding behavior locally before opening this PR.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New features and API changes have release notes
  • [N/A] Documentation complies with general and docstring guidelines

Initialize padded vertex storage with NaN instead of empty so unused entries cannot overflow during projection before invalid masking.

Co-authored-by: Cursor <cursoragent@cursor.com>

@akhmerov akhmerov 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.

I checked this PR and the fix looks appropriate.

Also to emphasize its usefulness: uninitialized data is also incorrectly used in computing the padding.

Comment on lines +130 to +135
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.add_collection(collection, autolim=False)
ax.set(xlim=(0, 1), ylim=(0, 1), zlim=(0, 1))
with np.errstate(over="raise"):
fig.canvas.draw()

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.

I don't think this half is a robust test. With np.empty, the unused parts could be large and overflow, but they could equally be 0 and do nothing strange, so this may or may not fail. I think we can just keep the first half of the test.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Ragged Poly3DCollection projects uninitialized padding, causing nondeterministic overflow

4 participants