Title: FIX: Prevent crash when removing a subfigure containing subplots by Vikash-Kumar-23 · Pull Request #31323 · matplotlib/matplotlib · GitHub
Open Graph Title: FIX: Prevent crash when removing a subfigure containing subplots by Vikash-Kumar-23 · Pull Request #31323 · matplotlib/matplotlib
X Title: FIX: Prevent crash when removing a subfigure containing subplots by Vikash-Kumar-23 · Pull Request #31323 · matplotlib/matplotlib
Description: 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 #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
Open Graph Description: 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()` ...
X Description: 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()` ...
Opengraph URL: https://github.com/matplotlib/matplotlib/pull/31323
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:56c512a3-f92a-b649-cc4f-21d81fa9ce2d |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | DD2A:3B4A30:1BBFA:23C0F:6A52D2AE |
| html-safe-nonce | 6e3aafb8cffb9e078b360441b28e528149281b414388714386a61585efc4dca5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDJBOjNCNEEzMDoxQkJGQToyM0MwRjo2QTUyRDJBRSIsInZpc2l0b3JfaWQiOiIyMDA5MDk5MTc1MzE1OTEzNDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1f3a6ba7b013ab170ed400aaec5a12e011d8c8036c6e70b7402feeccceb9cda8 |
| hovercard-subject-tag | pull_request:3413011941 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/matplotlib/matplotlib/pull/31323/files |
| twitter:image | https://avatars.githubusercontent.com/u/163628932?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/163628932?s=400&v=4 |
| og:image:alt | 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()` ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width