1414
1515"""
1616
17- import sys
17+ import sys , warnings
1818
1919import matplotlib
2020from matplotlib import _pylab_helpers , interactive
@@ -264,6 +264,14 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
264264
265265 figure(1)
266266
267+ The same applies if *num* is a string. In this case *num* will be used
268+ as an explicit figure label::
269+
270+ figure("today")
271+
272+ and in windowed backends, the window title will be set to this figure
273+ label.
274+
267275 If you are creating many figures, make sure you explicitly call "close"
268276 on the figures you are not using, because this will enable pylab
269277 to properly clean up the memory.
@@ -294,16 +302,29 @@ class that will be passed on to :meth:`new_figure_manager` in the
294302 if facecolor is None : facecolor = rcParams ['figure.facecolor' ]
295303 if edgecolor is None : edgecolor = rcParams ['figure.edgecolor' ]
296304
305+ allnums = get_fignums ()
306+ figLabel = ''
297307 if num is None :
298- allnums = [f .num for f in _pylab_helpers .Gcf .get_all_fig_managers ()]
299308 if allnums :
300309 num = max (allnums ) + 1
301310 else :
302311 num = 1
312+ elif is_string_like (num ):
313+ figLabel = num
314+ allLabels = get_figlabels ()
315+ if figLabel not in allLabels :
316+ if figLabel == 'all' :
317+ warnings .warn ("close('all') closes all existing figures" )
318+ if len (allLabels ):
319+ num = max (allnums ) + 1
320+ else :
321+ num = 1
322+ else :
323+ inum = allLabels .index (figLabel )
324+ num = allnums [inum ]
303325 else :
304326 num = int (num ) # crude validation of num argument
305327
306-
307328 figManager = _pylab_helpers .Gcf .get_fig_manager (num )
308329 if figManager is None :
309330 if get_backend ().lower () == 'ps' : dpi = 72
@@ -316,6 +337,10 @@ class that will be passed on to :meth:`new_figure_manager` in the
316337 FigureClass = FigureClass ,
317338 ** kwargs )
318339
340+ if figLabel :
341+ figManager .set_window_title (figLabel )
342+ figManager .canvas .figure .set_label (figLabel )
343+
319344 # make this figure current on button press event
320345 def make_active (event ):
321346 _pylab_helpers .Gcf .set_active (figManager )
@@ -346,6 +371,12 @@ def get_fignums():
346371 fignums .sort ()
347372 return fignums
348373
374+ def get_figlabels ():
375+ "Return a list of existing figure labels."
376+ figManagers = _pylab_helpers .Gcf .get_all_fig_managers ()
377+ figManagers .sort (key = lambda m : m .num )
378+ return [m .canvas .figure .get_label () for m in figManagers ]
379+
349380def get_current_fig_manager ():
350381 figManager = _pylab_helpers .Gcf .get_active ()
351382 if figManager is None :
@@ -367,9 +398,11 @@ def close(*args):
367398
368399 ``close()`` by itself closes the current figure
369400
401+ ``close(h)`` where *h* is a :class:`Figure` instance, closes that figure
402+
370403 ``close(num)`` closes figure number *num*
371404
372- ``close(h )`` where *h * is a :class:`Figure` instance , closes that figure
405+ ``close(name )`` where *name * is a string , closes figure with that label
373406
374407 ``close('all')`` closes all the figure windows
375408 """
@@ -385,6 +418,11 @@ def close(*args):
385418 _pylab_helpers .Gcf .destroy_all ()
386419 elif isinstance (arg , int ):
387420 _pylab_helpers .Gcf .destroy (arg )
421+ elif is_string_like (arg ):
422+ allLabels = get_figlabels ()
423+ if arg in allLabels :
424+ num = get_fignums ()[allLabels .index (arg )]
425+ _pylab_helpers .Gcf .destroy (num )
388426 elif isinstance (arg , Figure ):
389427 _pylab_helpers .Gcf .destroy_fig (arg )
390428 else :
0 commit comments