Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2938b41

Browse files
FIX: Prevent crash when removing a subfigure containing subplots (#31323)
* FIX: Prevent crash when removing a subfigure containing subplots * Use attribute-based guard in Artist.remove
1 parent 49fd492 commit 2938b41

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

lib/matplotlib/artist.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,12 @@ def remove(self):
255255
# clear stale callback
256256
self.stale_callback = None
257257
_ax_flag = False
258-
if hasattr(self, 'axes') and self.axes:
258+
ax = getattr(self, 'axes', None)
259+
mouseover_set = getattr(ax, '_mouseover_set', None)
260+
if mouseover_set is not None:
259261
# remove from the mouse hit list
260-
self.axes._mouseover_set.discard(self)
261-
self.axes.stale = True
262+
mouseover_set.discard(self)
263+
ax.stale = True
262264
self.axes = None # decouple the artist from the Axes
263265
_ax_flag = True
264266

lib/matplotlib/tests/test_figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,7 @@ def test_subfigures_wspace_hspace():
15761576
def test_subfigure_remove():
15771577
fig = plt.figure()
15781578
sfs = fig.subfigures(2, 2)
1579+
sfs[1, 1].subplots()
15791580
sfs[1, 1].remove()
15801581
assert len(fig.subfigs) == 3
15811582

0 commit comments

Comments
 (0)