Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94cf18d

Browse files
committed
Merge pull request #147 from butterw/master
Named Figures feature: plot('today')
2 parents a96422e + 4d45160 commit 94cf18d

6 files changed

Lines changed: 444 additions & 4 deletions

File tree

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ def tk_window_focus():
967967
'matplotlib.tests.test_mlab',
968968
'matplotlib.tests.test_transforms',
969969
'matplotlib.tests.test_axes',
970+
'matplotlib.tests.test_figure',
970971
'matplotlib.tests.test_dates',
971972
'matplotlib.tests.test_spines',
972973
'matplotlib.tests.test_image',

lib/matplotlib/pyplot.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
"""
1616

17-
import sys
17+
import sys, warnings
1818

1919
import matplotlib
2020
from 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+
349380
def 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:
Binary file not shown.
23.1 KB
Loading

0 commit comments

Comments
 (0)