Skip to content

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

Open
buddy0452004 wants to merge 3 commits intomatplotlib:mainfrom
buddy0452004:fix-contour-remove-crash
Open

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

Conversation

@buddy0452004
Copy link
Copy Markdown

@buddy0452004 buddy0452004 commented Mar 31, 2026

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.

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
  • [N/A] 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

@github-actions github-actions bot added topic: contour Documentation: examples files in galleries/examples labels Mar 31, 2026
@Chirag3841
Copy link
Copy Markdown
Contributor

@buddy0452004 I was working on a similar fix based on the discussion here.
It might be good to add a test file for this case too .

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Crash when removing contour set after removing contour labels

2 participants