GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — FIX: Prevent crash when removing a subfigure containing subplots by Vikash-Kumar-23 · Pull Request #31323 · matplotlib/matplotlib

Conversation

@Vikash-Kumar-23

@Vikash-Kumar-23 Vikash-Kumar-23 commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Closes #31319

## PR summary
This PR fixes a crash reported in #31319 that occurs when removing a subfigure after creating subplots inside it.

### Why is this change necessary?
Calling `.remove()` on a `SubFigure` can currently raise:
```pytb
AttributeError: 'list' object has no attribute '_mouseover_set'

This makes subfigure removal unreliable in otherwise valid figure layouts.

What problem does it solve?

It prevents Artist.remove() from attempting axes-specific mouse-hitlist cleanup when self.axes is not an actual matplotlib.axes.Axes instance (it can be a list in the subfigure-removal scenario).

What is the reasoning for this implementation?

Artist.remove() contains cleanup code that assumes self.axes is an Axes and accesses self.axes._mouseover_set. In the failing case, self.axes is a list, so accessing _mouseover_set crashes. The fix tightens the guard so this cleanup block only runs when self.axes is a non-None Axes instance.

Minimal self-contained example (repro / verification)

import matplotlib
matplotlib.use("Agg")  # for reproducibility/headless runs
import matplotlib.pyplot as plt

fig = plt.figure()
subfigs = fig.subfigures(1, 2)
subfigs[0].subplots()
subfigs[0].remove()
print("OK: no crash")

What changed (only 2 files)

  • lib/matplotlib/artist.py: Guard the _mouseover_set cleanup in Artist.remove() so it only runs when self.axes is an Axes.
  • lib/matplotlib/tests/test_figure.py: Add/update a regression test that creates subplots inside a subfigure and then removes the subfigure.

How to test locally

python -m pytest lib/matplotlib/tests/test_figure.py -k test_subfigure_remove -q

AI Disclosure

I used AI assistance to help draft the PR description and verification steps. The code changes and local verification were performed and reviewed by me.

PR checklist

  • "closes [Bug]: Crash when removing a subfigure with a subplot in a figure #31319" 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Vikash-Kumar-23
FIX: Prevent crash when removing a subfigure containing subplots
702b429
@github-actions

github-actions Bot commented Mar 18, 2026

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the first-contribution label Mar 18, 2026
@github-project-automation github-project-automation Bot added this to First Time Contributors Mar 18, 2026
@tacaswell

tacaswell commented Mar 18, 2026

Copy link
Copy Markdown
Member

Do we need to go up a layer in the inheritance tree? Will this work with the axes in mpl toolkits?

Would checking that it is not a list be a better option? Still has some edge cases, but does not introduce circular imports.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Vikash-Kumar-23
Use attribute-based guard in Artist.remove
270287d
@Vikash-Kumar-23

Vikash-Kumar-23 commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I’ve updated the guard in Artist.remove() to avoid importing or type-checking against Axes.

Instead of isinstance(self.axes, Axes), it now uses an attribute-based check:

  • ax = getattr(self, "axes", None)
  • mouseover_set = getattr(ax, "_mouseover_set", None)

The cleanup now only runs when _mouseover_set is present. This avoids the crash when self.axes is a list, prevents circular import issues, and should work with toolkit/derived axes as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@melissawm melissawm moved this to Needs review in First Time Contributors Mar 18, 2026
QuLogic
QuLogic approved these changes Mar 26, 2026
View reviewed changes
@melissawm melissawm moved this from Needs review to Needs decision in First Time Contributors Mar 27, 2026
rcomer
rcomer approved these changes Mar 27, 2026
View reviewed changes
@rcomer rcomer added this to the v3.10.9 milestone Mar 27, 2026
@rcomer
rcomer merged commit 2938b41 into matplotlib:main Mar 27, 2026
48 of 52 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@github-project-automation github-project-automation Bot moved this from Needs decision to Merged in First Time Contributors Mar 27, 2026
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Mar 27, 2026
@Vikash-Kumar-23 @meeseeksmachine
Backport PR matplotlib#31323: FIX: Prevent crash when removing a subf…
f32af1d
…igure containing subplots
@meeseeksmachine meeseeksmachine mentioned this pull request Mar 27, 2026
Backport PR #31323 on branch v3.10.x (FIX: Prevent crash when removing a subfigure containing subplots) #31411
Merged
@rcomer

rcomer commented Mar 27, 2026

Copy link
Copy Markdown
Member

Thanks @Vikash-Kumar-23 and congratulations on your first Matplotlib PR 🎉 Hopefully we will here from you again.

Vikash-Kumar-23 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rcomer added a commit that referenced this pull request Mar 29, 2026
@rcomer
Merge pull request #31411 from meeseeksmachine/auto-backport-of-pr-31…
51c85f7
…323-on-v3.10.x

Backport PR #31323 on branch v3.10.x (FIX: Prevent crash when removing a subfigure containing subplots)
This was referenced Apr 21, 2026
Backport PR #31282 on branch v3.10.x (SEC: Block shell escapes in latex and ps commands) #31542
Closed
Backport PR #31248 on branch v3.10.x (SEC: Remove eval() from validate_cycler) #31543
Closed
merge up v3.10.9 #31560
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@QuLogic QuLogic QuLogic approved these changes
@rcomer rcomer rcomer approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

first-contribution

Projects

First Time Contributors
Status: Merged

Milestone

v3.10.9

Development

Successfully merging this pull request may close these issues.

[Bug]: Crash when removing a subfigure with a subplot in a figure

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@Vikash-Kumar-23 @tacaswell @rcomer @QuLogic
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.