Skip to content

Commit 86950c2

Browse files
committed
MNT: greatly simplify how figures are "numbered"
1 parent 70f9d55 commit 86950c2

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

mpl_gui/__init__.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ def _register_fig(self, fig):
143143
fignum = next(self._count)
144144
if fig.get_label() == "":
145145
fig.set_label(f"{self._prefix}{fignum:d}")
146+
# TODO: is there a better way to track this than monkey patching?
147+
fig._mpl_gui_fignum = fignum
146148
return fig
147149

148150
@property
@@ -171,7 +173,7 @@ def by_number(self):
171173
Return a dictionary of the current mapping number -> figures.
172174
173175
"""
174-
self._promote_and_number()
176+
self._ensure_all_figures_promoted()
175177
return {fig.canvas.manager.num: fig for fig in self.figures}
176178

177179
@functools.wraps(figure)
@@ -189,17 +191,10 @@ def subplot_mosaic(self, *args, **kwargs):
189191
fig, axd = subplot_mosaic(*args, **kwargs)
190192
return self._register_fig(fig), axd
191193

192-
def _promote_and_number(self):
193-
promoted_figures = []
194-
unpromotod_figures = []
194+
def _ensure_all_figures_promoted(self):
195195
for f in self.figures:
196-
if f.canvas.manager is not None:
197-
promoted_figures.append(f)
198-
else:
199-
unpromotod_figures.append(f)
200-
next_num = max([f.canvas.manager.num for f in promoted_figures], default=0) + 1
201-
for num, fig in enumerate(unpromotod_figures, start=next_num):
202-
promote_figure(fig, num=num)
196+
if f.canvas.manager is None:
197+
promote_figure(f, num=f._mpl_gui_fignum)
203198

204199
def show_all(self, *, block=None, timeout=None):
205200
"""
@@ -232,7 +227,7 @@ def show_all(self, *, block=None, timeout=None):
232227

233228
if timeout is None:
234229
timeout = self._timeout
235-
self._promote_and_number()
230+
self._ensure_all_figures_promoted()
236231
show(self.figures, block=self._block, timeout=self._timeout)
237232

238233
# alias to easy pyplot compatibility

0 commit comments

Comments
 (0)