FIX: Prevent crash when removing a subfigure containing subplots#31323
FIX: Prevent crash when removing a subfigure containing subplots#31323Vikash-Kumar-23 wants to merge 2 commits intomatplotlib:mainfrom
Conversation
|
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. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
|
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. |
|
Thanks for the feedback! I’ve updated the guard in Instead of
The cleanup now only runs when |
Closes #31319
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 whenself.axesis not an actualmatplotlib.axes.Axesinstance (it can be alistin the subfigure-removal scenario).What is the reasoning for this implementation?
Artist.remove()contains cleanup code that assumesself.axesis anAxesand accessesself.axes._mouseover_set. In the failing case,self.axesis alist, so accessing_mouseover_setcrashes. The fix tightens the guard so this cleanup block only runs whenself.axesis a non-NoneAxesinstance.Minimal self-contained example (repro / verification)
What changed (only 2 files)
lib/matplotlib/artist.py: Guard the_mouseover_setcleanup inArtist.remove()so it only runs whenself.axesis anAxes.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
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