pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/matplotlib/matplotlib/pull/31431

obal-0bd78641c0a1f3e0.css" /> FIX: Guard against already-removed labels in ContourSet.remove() by buddy0452004 · Pull Request #31431 · matplotlib/matplotlib · GitHub
Skip to content

FIX: Guard against already-removed labels in ContourSet.remove()#31431

Draft
buddy0452004 wants to merge 4 commits intomatplotlib:mainfrom
buddy0452004:fix-contour-remove-crash-v2
Draft

FIX: Guard against already-removed labels in ContourSet.remove()#31431
buddy0452004 wants to merge 4 commits intomatplotlib:mainfrom
buddy0452004:fix-contour-remove-crash-v2

Conversation

@buddy0452004
Copy link
Copy Markdown

When contour labels are manually removed before calling CS.remove(), matplotlib still tries to remove them again internally, causing a ValueError.

The root cause is that super().remove() detaches the contour from the axes, setting self.axes to None. Any subsequent access to self.axes.texts then crashes. The fix saves the axes reference before calling super().remove(), then uses it safely with a None check.

Also adds a regression test for this case.

Closes #31404

AI Disclosure

No AI used.

PR Checklist

  • "closes [Bug]: Crash when removing contour set after removing contour labels #31404" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New Features and API Changes are noted with a directive and release note
  • [N/A] Documentation complies with general and docstring guidelines

Copy link
Copy Markdown
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

Thanks @buddy0452004, but I do not think you understood the root cause of why the failure happens. I think the explanation at #31404 (comment) is correct. The error shown in the issue states that we tried to remove something from a list that is not in the list. So we got hold of the list axes.texts without problem, but the labels were no longer in there.


def test_contour_remove_after_label_removed():
# Test that CS.remove() works even if labels were manually removed first
# Regression test for https://github.com/matplotlib/matplotlib/issues/31404
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.

We do not typically reference issues in test comments.

x = np.linspace(-3.0, 3.0, 20)
y = np.linspace(-2.0, 2.0, 20)
X, Y = np.meshgrid(x, y)
Z = np.exp(-X**2 - Y**2)
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.

For efficiency, try to use the simplest data input you can that still makes the test effective. Look at test_contourf_rasterize for an example.

Copy link
Copy Markdown
Contributor

@Chirag3841 Chirag3841 Apr 1, 2026

Choose a reason for hiding this comment

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

@rcomer Yes, that makes sense , I had also suggested using simpler input data in the discussion #31404 .




def test_contour_remove_after_label_removed():
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.

Since there is already a test_contour_remove further up the file, I would put this one right below it, to keep tests with similar purposes together.

for text in self.labelTexts:
text.remove()
for text in list(self.labelTexts):
if axes is not None and text in axes.texts:
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.

Why do we check whether axes is None here?

@buddy0452004
Copy link
Copy Markdown
Author

@rcomer Thanks for the review! You're right about the root cause the issue is that the labels were already removed from axes.texts, not that axes is None. Updated the fix to use try/except ValueError instead, which is simpler and correctly addresses the actual failure.

Also:

  • Removed the issue reference from the test comment
  • Simplified test data to use np.arange(16).reshape((4, 4))
  • Moved the test right below test_contour_remove

@timhoffm
Copy link
Copy Markdown
Member

timhoffm commented Apr 1, 2026

I'm putting this on hold until we have clarified what is actually needed. #31404 (comment)

@timhoffm timhoffm marked this pull request as draft April 1, 2026 12:31
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]: Crash when removing contour set after removing contour labels

4 participants

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy