Skip to content

Commit 51c85f7

Browse files
authored
Merge pull request #31411 from meeseeksmachine/auto-backport-of-pr-31323-on-v3.10.x
Backport PR #31323 on branch v3.10.x (FIX: Prevent crash when removing a subfigure containing subplots)
2 parents b9163f8 + f32af1d commit 51c85f7

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

lib/matplotlib/artist.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,12 @@ def remove(self):
240240
# clear stale callback
241241
self.stale_callback = None
242242
_ax_flag = False
243-
if hasattr(self, 'axes') and self.axes:
243+
ax = getattr(self, 'axes', None)
244+
mouseover_set = getattr(ax, '_mouseover_set', None)
245+
if mouseover_set is not None:
244246
# remove from the mouse hit list
245-
self.axes._mouseover_set.discard(self)
246-
self.axes.stale = True
247+
mouseover_set.discard(self)
248+
ax.stale = True
247249
self.axes = None # decouple the artist from the Axes
248250
_ax_flag = True
249251

lib/matplotlib/tests/test_figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,7 @@ def test_subfigures_wspace_hspace():
15511551
def test_subfigure_remove():
15521552
fig = plt.figure()
15531553
sfs = fig.subfigures(2, 2)
1554+
sfs[1, 1].subplots()
15541555
sfs[1, 1].remove()
15551556
assert len(fig.subfigs) == 3
15561557

0 commit comments

Comments
 (0)