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

Skip to content

Commit 770d0a0

Browse files
committed
fixed a close(fig) bug
svn path=/trunk/matplotlib/; revision=909
1 parent c17e217 commit 770d0a0

File tree

7 files changed

+78
-39
lines changed

7 files changed

+78
-39
lines changed

API_CHANGES

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ API CHANGES in matplotlib-0.72
33
Line2D, Text, and Patch copy_properties renamed update_from and
44
moved into artist base class
55

6-
LineCollecitons.color renamed to LineCollections.set_color for
7-
consistency with set/get introspection mechanism,
6+
LineCollecitons.color renamed to LineCollections.set_color for
7+
consistency with set/get introspection mechanism,
88

9+
pylab figure now defaults to num=None, which creates a new figure
10+
with a guaranteed unique number
911

1012
API CHANGES in matplotlib-0.71
1113

CHANGELOG

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ New entries should be added at the top
22

33
2005-02-01 Added Fernando's figure num patch, including experemental
44
support for pylab backend switching, LineCOllection.color
5-
warns - JDH
5+
warns, savefig now a figure method, fixed a close(fig) bug
6+
- JDH
67

78
2005-01-31 updated datalim in contour - JDH
89

@@ -28,7 +29,7 @@ New entries should be added at the top
2829
2005-01-24 Fixed a mathtext parser bug that prevented font changes in
2930
sub/superscripts - JDH
3031

31-
2005-01-24 Fixed contour wto work w/ interactive changes in colormaps,
32+
2005-01-24 Fixed contour to work w/ interactive changes in colormaps,
3233
clim, etc - JDH
3334

3435
===============================================================

examples/agg_oo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
"""
3+
A pure OO (look Ma, no pylab!) example using the agg backend
4+
"""
5+
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
6+
from matplotlib.figure import Figure
7+
8+
fig = Figure()
9+
canvas = FigureCanvas(fig)
10+
ax = fig.add_subplot(111)
11+
ax.plot([1,2,3])
12+
ax.set_title('hi mom')
13+
ax.grid(True)
14+
ax.set_xlabel('time')
15+
ax.set_ylabel('volts')
16+
canvas.print_figure('test')

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ class FigureCanvasBase:
662662
)
663663

664664
def __init__(self, figure):
665+
figure.set_canvas(self)
665666
self.figure = figure
666667
self.cid = 0
667668
# a dictionary from event name to a dictionary that maps cid->func

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ def destroy(self, *args):
286286
if self.window is not None:
287287
#print 'calling window destroy'
288288
self.window.destroy()
289+
pass
289290
self.window = None
290291

291292
class AxisMenu:

lib/matplotlib/figure.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ def __init__(self,
5959
self._set_artist_props(self.figurePatch)
6060

6161
self._hold = rcParams['axes.hold']
62+
self.canvas = None
6263
self.clf()
6364

65+
def set_canvas(self, canvas):
66+
'set the canvas the contains the figure'
67+
self.canvas = canvas
6468

6569
def hold(self, b=None):
6670
"""
@@ -427,3 +431,26 @@ def add_axobserver(self, func):
427431
'whenever the axes state change, func(self) will be called'
428432
self._axobservers.append(func)
429433

434+
435+
def savefig(self, *args, **kwargs):
436+
"""
437+
SAVEFIG(fname, dpi=150, facecolor='w', edgecolor='w',
438+
orientation='portrait'):
439+
440+
Save the current figure to filename fname. dpi is the resolution
441+
in dots per inch.
442+
443+
Output file types currently supported are jpeg and png and will be
444+
deduced by the extension to fname
445+
446+
facecolor and edgecolor are the colors os the figure rectangle
447+
448+
orientation is either 'landscape' or 'portrait' - not supported on
449+
all backends; currently only on postscript output."""
450+
451+
for key in ('dpi', 'facecolor', 'edgecolor'):
452+
if not kwargs.has_key(key):
453+
kwargs[key] = rcParams['savefig.%s'%key]
454+
455+
self.canvas.print_figure(*args, **kwargs)
456+

lib/matplotlib/pylab.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,10 @@ def close(*args):
610610
_pylab_helpers.Gcf.destroy(arg)
611611
elif isinstance(arg, Figure):
612612
for manager in _pylab_helpers.Gcf.get_all_fig_managers():
613-
if manager.figure==arg:
613+
if manager.canvas.figure==arg:
614614
_pylab_helpers.Gcf.destroy(manager.num)
615615
else:
616-
error_msg('Unrecognized argument type to close')
616+
error_msg('Unrecognized argument type %s to close'%type(arg))
617617
else:
618618
error_msg('close takes 0 or 1 arguments')
619619

@@ -757,6 +757,15 @@ def figlegend(handles, labels, loc, **kwargs):
757757
l= gcf().legend(handles, labels, loc, **kwargs)
758758
draw_if_interactive()
759759
return l
760+
761+
def savefig(*args, **kwargs):
762+
try: ret = gcf().savefig(*args, **kwargs)
763+
except RuntimeError, msg:
764+
msg = raise_msg_to_str(msg)
765+
error_msg(msg)
766+
raise RuntimeError(msg)
767+
else: return ret
768+
savefig.__doc__ = Figure.savefig.__doc__
760769

761770

762771
def figure(num=None, # autoincrement if None, else integer from 1-N
@@ -767,23 +776,27 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
767776
frameon = True,
768777
):
769778
"""
770-
figure(num = 1, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
779+
figure(num = None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
780+
771781
782+
Create a new figure and return a handle to it. If num=None, the
783+
figure number will be incremented and a new figure will be
784+
created.
772785
773-
Create a new figure and return a handle to it
774786
775-
If figure(num) already exists, make it active and return the
776-
handle to it.
787+
If num is an integer, and figure(num) already exists, make it
788+
active and return the handle to it. If figure(num) does not exist
789+
it will be created. Numbering starts at 1, matlab style
777790
778791
figure(1)
779792
780-
If num=None, the figure number will be incremented and a new
781-
figure will be created.
782-
783-
figsize - width in height x inches; defaults to rc figure.figsize
784-
dpi - resolution; defaults to rc figure.dpi
785-
facecolor - the background color; defaults to rc figure.facecolor
786-
edgecolor - the border color; defaults to rc figure.edgecolor
793+
794+
kwargs:
795+
796+
figsize - width in height x inches; defaults to rc figure.figsize
797+
dpi - resolution; defaults to rc figure.dpi
798+
facecolor - the background color; defaults to rc figure.facecolor
799+
edgecolor - the border color; defaults to rc figure.edgecolor
787800
788801
rcParams gives the default values from the .matplotlibrc file
789802
@@ -1019,28 +1032,6 @@ def save(fname, X, fmt='%1.4f'):
10191032
if origShape is not None:
10201033
X.shape = origShape
10211034

1022-
def savefig(*args, **kwargs):
1023-
"""
1024-
SAVEFIG(fname, dpi=150, facecolor='w', edgecolor='w',
1025-
orientation='portrait'):
1026-
1027-
Save the current figure to filename fname. dpi is the resolution
1028-
in dots per inch.
1029-
1030-
Output file types currently supported are jpeg and png and will be
1031-
deduced by the extension to fname
1032-
1033-
facecolor and edgecolor are the colors os the figure rectangle
1034-
1035-
orientation is either 'landscape' or 'portrait' - not supported on
1036-
all backends; currently only on postscript output."""
1037-
1038-
for key in ('dpi', 'facecolor', 'edgecolor'):
1039-
if not kwargs.has_key(key):
1040-
kwargs[key] = rcParams['savefig.%s'%key]
1041-
1042-
manager = get_current_fig_manager()
1043-
manager.canvas.print_figure(*args, **kwargs)
10441035

10451036
class _ObjectInspector:
10461037

0 commit comments

Comments
 (0)