13
13
14
14
"""
15
15
from collections import Counter
16
- from itertools import count
17
16
import functools
18
17
import logging
19
18
import warnings
19
+ from weakref import WeakKeyDictionary
20
20
21
21
from matplotlib .backend_bases import FigureCanvasBase as _FigureCanvasBase
22
22
@@ -118,7 +118,7 @@ def __init__(self, *, block=None, timeout=0, prefix="Figure "):
118
118
self ._timeout = timeout
119
119
self ._block = block
120
120
# Settings / state to control the default figure label
121
- self ._count = count ()
121
+ self ._fig_to_number = WeakKeyDictionary ()
122
122
self ._prefix = prefix
123
123
# the canonical location for storing the Figures this registry owns.
124
124
# any additional views must never include a figure not in the list but
@@ -140,11 +140,10 @@ def _register_fig(self, fig):
140
140
# empty string) on a Figure so if they provide duplicate labels, change
141
141
# the labels under us, or provide a label that will be shadowed in the
142
142
# future it will be what it is.
143
- fignum = next (self ._count )
143
+ fignum = max (self ._fig_to_number . values (), default = 0 ) + 1
144
144
if fig .get_label () == "" :
145
145
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
146
+ self ._fig_to_number [fig ] = fignum
148
147
return fig
149
148
150
149
@property
@@ -194,7 +193,7 @@ def subplot_mosaic(self, *args, **kwargs):
194
193
def _ensure_all_figures_promoted (self ):
195
194
for f in self .figures :
196
195
if f .canvas .manager is None :
197
- promote_figure (f , num = f . _mpl_gui_fignum )
196
+ promote_figure (f , num = self . _fig_to_number [ f ] )
198
197
199
198
def show_all (self , * , block = None , timeout = None ):
200
199
"""
0 commit comments