Skip to content

Commit 90c2a8b

Browse files
committed
ENH: remove figures from registry when they are closed
1 parent 1db1ab5 commit 90c2a8b

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

mpl_gui/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,28 @@ def __init__(self, *, block=None, timeout=0):
118118
self._block = block
119119
self.figures = []
120120

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+
121129
@functools.wraps(figure)
122130
def figure(self, *args, **kwargs):
123131
fig = figure(*args, **kwargs)
124-
self.figures.append(fig)
125-
return fig
132+
return self._register_fig(fig)
126133

127134
@functools.wraps(subplots)
128135
def subplots(self, *args, **kwargs):
129136
fig, axs = subplots(*args, **kwargs)
130-
self.figures.append(fig)
131-
return fig, axs
137+
return self._register_fig(fig), axs
132138

133139
@functools.wraps(subplot_mosaic)
134140
def subplot_mosaic(self, *args, **kwargs):
135141
fig, axd = subplot_mosaic(*args, **kwargs)
136-
self.figures.append(fig)
137-
return fig, axd
142+
return self._register_fig(fig), axd
138143

139144
def show_all(self, *, block=None, timeout=None):
140145
"""

0 commit comments

Comments
 (0)