@@ -118,23 +118,28 @@ def __init__(self, *, block=None, timeout=0):
118
118
self ._block = block
119
119
self .figures = []
120
120
121
+ def _register_fig (self , fig ):
122
+ fig .canvas .mpl_connect (
123
+ "close_event" ,
124
+ lambda e : self .figures .remove (fig ) if fig in self .figures else None ,
125
+ )
126
+ self .figures .append (fig )
127
+ return fig
128
+
121
129
@functools .wraps (figure )
122
130
def figure (self , * args , ** kwargs ):
123
131
fig = figure (* args , ** kwargs )
124
- self .figures .append (fig )
125
- return fig
132
+ return self ._register_fig (fig )
126
133
127
134
@functools .wraps (subplots )
128
135
def subplots (self , * args , ** kwargs ):
129
136
fig , axs = subplots (* args , ** kwargs )
130
- self .figures .append (fig )
131
- return fig , axs
137
+ return self ._register_fig (fig ), axs
132
138
133
139
@functools .wraps (subplot_mosaic )
134
140
def subplot_mosaic (self , * args , ** kwargs ):
135
141
fig , axd = subplot_mosaic (* args , ** kwargs )
136
- self .figures .append (fig )
137
- return fig , axd
142
+ return self ._register_fig (fig ), axd
138
143
139
144
def show_all (self , * , block = None , timeout = None ):
140
145
"""
0 commit comments