@@ -252,6 +252,37 @@ def close_all(self):
252
252
self .close (fig )
253
253
254
254
def close (self , val ):
255
+ """
256
+ Close (meaning destroy the UI) and forget a managed Figure.
257
+
258
+ This will do two things:
259
+
260
+ - start the destruction process of an UI (the event loop may need to
261
+ run to complete this process and if the user is holding hard
262
+ references to any of the UI elements they may remain alive).
263
+ - Remove the `Figure` from this Registry.
264
+
265
+ We will no longer have any hard references to the Figure, but if
266
+ the user does the `Figure` (and its components) will not be garbage
267
+ collected. Due to the circular references in Matplotlib these
268
+ objects may not be collected until the full cyclic garbage collection
269
+ runs.
270
+
271
+ If the user still has a reference to the `Figure` they can re-show the
272
+ figure via `show`, but the `FigureRegistry` will not be aware of it.
273
+
274
+ Parameters
275
+ ----------
276
+ val : 'all' or int or str or Figure
277
+
278
+ - The special case of 'all' closes all open Figures
279
+ - If any other string is passed, it is interpreted as a key in
280
+ `by_label` and that Figure is closed
281
+ - If an integer it is interpreted as a key in `by_number` and that
282
+ Figure is closed
283
+ - If it is a `Figure` instance, then that figure is closed
284
+
285
+ """
255
286
if val == "all" :
256
287
return self .close_all ()
257
288
# or do we want to close _all_ of the figures with a given label / number?
@@ -261,12 +292,17 @@ def close(self, val):
261
292
fig = self .by_number [val ]
262
293
else :
263
294
fig = val
295
+ if fig not in self .figures :
296
+ raise ValueError (
297
+ "Trying to close a figure not associated with this Registry."
298
+ )
264
299
if fig .canvas .manager is not None :
265
300
fig .canvas .manager .destroy ()
266
301
# disconnect figure from canvas
267
302
fig .canvas .figure = None
268
303
# disconnect canvas from figure
269
304
_FigureCanvasBase (figure = fig )
305
+ assert fig .canvas .manager is None
270
306
if fig in self .figures :
271
307
self .figures .remove (fig )
272
308
0 commit comments