Permalink
Browse files

pyplot restore support added. Further backends will need modification…

… to mirror TkAgg.
  • Loading branch information...
1 parent 0db9429 commit 3ec63ad863a69ce8a5098bd31cb9434e646137c8 @pelson committed Jul 17, 2012
View
@@ -70,6 +70,16 @@ minimum and maximum colorbar extensions.
plt.show()
+Figures are picklable
+---------------------
+
+Philip Elson made figures picklable for quick storage of plots. Pickle files
+are not designed for long term storage, are unsupported when restoring a pickle
+saved in another matplotlib version and are insecure when restoring a pickle
+from an untrusted source. Having said this, they are useful for short term
+storage for later modification inside matplotlib.
+
+
Set default bounding box in matplotlibrc
------------------------------------------
@@ -14,7 +14,7 @@ def error_msg(msg):
class Gcf(object):
"""
- Manage a set of integer-numbered figures.
+ Singleton to manage a set of integer-numbered figures.
This class is never instantiated; it consists of two class
attributes (a list and a dictionary), and a set of static
@@ -131,6 +131,7 @@ def set_active(manager):
if m != manager: Gcf._activeQue.append(m)
Gcf._activeQue.append(manager)
Gcf.figs[manager.num] = manager
+
atexit.register(Gcf.destroy_all)
View
@@ -107,8 +107,8 @@ def __init__(self):
def __getstate__(self):
d = self.__dict__.copy()
# remove the unpicklable remove method, this will get re-added on load
- d.pop('_remove_method')
-# axes_artist_collections = ['lines', 'collections', 'tables', '']
+ # if the artist lives on an axes.
+ d['_remove_method'] = None
return d
def remove(self):
View
@@ -173,10 +173,12 @@ def __init__(self, axes, command='plot'):
self.set_color_cycle()
def __getinitargs__(self):
+ # note: __getinitargs__ only works for old-style classes
# means that the color cycle will be lost.
return (self.axes, self.command)
def __getstate__(self):
+ # We don't need any state as we have the init args
return False
def set_color_cycle(self, clist=None):
@@ -497,6 +499,15 @@ def __init__(self, fig, rect,
self._ycid = self.yaxis.callbacks.connect('units finalize',
self.relim)
+ def __setstate__(self, state):
+ self.__dict__ = state
+ # put the _remove_method back on all artists contained within the axes
+ for container_name in ['lines', 'collections', 'tables', 'patches',
+ 'texts', 'images']:
+ container = getattr(self, container_name)
+ for artist in container:
+ artist._remove_method = container.remove
+
def get_window_extent(self, *args, **kwargs):
"""
get the axes bounding box in display space; *args* and
@@ -1431,9 +1442,7 @@ def add_artist(self, a):
self.artists.append(a)
self._set_artist_props(a)
a.set_clip_path(self.patch)
- def remove_fn(artist):
- self.artists.remove(artist)
- a._remove_method = remove_fn #lambda h: self.artists.remove(h)
+ a._remove_method = lambda h: self.artists.remove(h)
return a
def add_collection(self, collection, autolim=True):
@@ -1455,11 +1464,7 @@ def add_collection(self, collection, autolim=True):
if collection._paths and len(collection._paths):
self.update_datalim(collection.get_datalim(self.transData))
- # XXX back to start
- def remove_fn(artist):
- self.collections.remove(artist)
-
- collection._remove_method = remove_fn #lambda h: self.collections.remove(h)
+ collection._remove_method = lambda h: self.collections.remove(h)
return collection
def add_line(self, line):
@@ -1477,10 +1482,7 @@ def add_line(self, line):
if not line.get_label():
line.set_label('_line%d'%len(self.lines))
self.lines.append(line)
-# def remove_fn(artist):
-# self.lines.remove(artist)
-# line._remove_method = remove_fn #lambda h: self.lines.remove(h)
- line._remove_method = self.lines.remove
+ line._remove_method = lambda h: self.lines.remove(h)
return line
def _update_line_limits(self, line):
@@ -1506,9 +1508,7 @@ def add_patch(self, p):
p.set_clip_path(self.patch)
self._update_patch_limits(p)
self.patches.append(p)
- def remove_fn(artist):
- self.patches.remove(artist)
- p._remove_method = remove_fn #lambda h: self.patches.remove(h)
+ p._remove_method = lambda h: self.patches.remove(h)
return p
def _update_patch_limits(self, patch):
@@ -1543,9 +1543,7 @@ def add_table(self, tab):
self._set_artist_props(tab)
self.tables.append(tab)
tab.set_clip_path(self.patch)
- def remove_fn(artist):
- self.tables.remove(artist)
- tab._remove_method = remove_fn #lambda h: self.tables.remove(h)
+ tab._remove_method = lambda h: self.tables.remove(h)
return tab
def add_container(self, container):
@@ -1559,9 +1557,7 @@ def add_container(self, container):
if not label:
container.set_label('_container%d'%len(self.containers))
self.containers.append(container)
- def remove_fn(artist):
- self.containers.remove(artist)
- container.set_remove_method(remove_fn)
+ container.set_remove_method(lambda h: self.containers.remove(container))
return container
@@ -3353,9 +3349,7 @@ def text(self, x, y, s, fontdict=None,
if fontdict is not None: t.update(fontdict)
t.update(kwargs)
self.texts.append(t)
- def remove_fn(artist):
- self.texts.remove(artist)
- t._remove_method = remove_fn #lambda h: self.texts.remove(h)
+ t._remove_method = lambda h: self.texts.remove(h)
#if t.get_clip_on(): t.set_clip_box(self.bbox)
@@ -3384,9 +3378,7 @@ def annotate(self, *args, **kwargs):
self._set_artist_props(a)
if kwargs.has_key('clip_on'): a.set_clip_path(self.patch)
self.texts.append(a)
- def remove_fn(artist):
- self.texts.remove(artist)
- a._remove_method = remove_fn #lambda h: self.texts.remove(h)
+ a._remove_method = lambda h: self.texts.remove(h)
return a
#### Lines and spans
@@ -7049,9 +7041,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
im.set_extent(im.get_extent())
self.images.append(im)
- def remove_fn(artist):
- self.images.remove(artist)
- im._remove_method = remove_fn #lambda h: self.images.remove(h)
+ im._remove_method = lambda h: self.images.remove(h)
return im
@@ -8894,7 +8884,7 @@ class _PicklableSubplotClassConstructor(object):
"""
This stub class exists to return the appropriate subplot
class when __call__-ed with an axes class. This is purely to
- allow Picking of Axes and Subplots."""
+ allow Pickling of Axes and Subplots."""
def __call__(self, axes_class):
# create a dummy object instance
subplot_instance = _PicklableSubplotClassConstructor()
@@ -52,6 +52,6 @@ def do_nothing(*args, **kwargs): pass
matplotlib.verbose.report('backend %s version %s' % (backend,backend_version))
- return new_figure_manager, draw_if_interactive, show
+ return backend_mod, new_figure_manager, draw_if_interactive, show
@@ -74,9 +74,16 @@ def new_figure_manager(num, *args, **kwargs):
"""
Create a new figure manager instance
"""
- _focus = windowing.FocusManager()
FigureClass = kwargs.pop('FigureClass', Figure)
figure = FigureClass(*args, **kwargs)
+ return new_figure_manager_given_figure(num, figure)
+
+
+def new_figure_manager_given_figure(num, figure):
+ """
+ Create a new figure manager instance for the given figure.
+ """
+ _focus = windowing.FocusManager()
window = Tk.Tk()
if Tk.TkVersion >= 8.5:
Oops, something went wrong.

0 comments on commit 3ec63ad

Please sign in to comment.