Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pickling support added. Various whitespace fixes as a result of reading *lots* of code.#1175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
efiring merged 3 commits intomatplotlib:masterfrompelson:pickle2
Sep 1, 2012
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Pickling support added. Various whitespace fixes as a result of readi…
…ng *lots* of code.
  • Loading branch information
@pelson
pelson committedAug 30, 2012
commit7885d944b594a319420be57c3c82f9254996a881
11 changes: 11 additions & 0 deletionsdoc/users/whats_new.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -100,6 +100,17 @@ minimum and maximum colorbar extensions.
plt.show()


Figures are picklable
---------------------

Philip Elson added an experimental feature to make figures picklable
for quick and easy short-term 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
------------------------------------------

Expand Down
1 change: 1 addition & 0 deletionslib/matplotlib/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1085,6 +1085,7 @@ def tk_window_focus():
'matplotlib.tests.test_mathtext',
'matplotlib.tests.test_mlab',
'matplotlib.tests.test_patches',
'matplotlib.tests.test_pickle',
'matplotlib.tests.test_rcparams',
'matplotlib.tests.test_simplification',
'matplotlib.tests.test_spines',
Expand Down
3 changes: 2 additions & 1 deletionlib/matplotlib/_pylab_helpers.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand DownExpand Up@@ -132,6 +132,7 @@ def set_active(manager):
Gcf._activeQue.append(manager)
Gcf.figs[manager.num] = manager


atexit.register(Gcf.destroy_all)


Expand Down
9 changes: 8 additions & 1 deletionlib/matplotlib/artist.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -103,6 +103,13 @@ def __init__(self):
self._gid = None
self._snap = None

def __getstate__(self):
d = self.__dict__.copy()
# remove the unpicklable remove method, this will get re-added on load
# (by the axes) if the artist lives on an axes.
d['_remove_method'] = None
return d

def remove(self):
"""
Remove the artist from the figure if possible. The effect
Expand All@@ -122,7 +129,7 @@ def remove(self):
# the _remove_method attribute directly. This would be a protected
# attribute if Python supported that sort of thing. The callback
# has one parameter, which is the child to be removed.
if self._remove_method!= None:
if self._remove_methodis not None:
self._remove_method(self)
else:
raise NotImplementedError('cannot remove artist')
Expand Down
47 changes: 44 additions & 3 deletionslib/matplotlib/axes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -153,9 +153,8 @@ def set_default_color_cycle(clist):
DeprecationWarning)


class _process_plot_var_args:
class _process_plot_var_args(object):
"""

Process variable length arguments to the plot command, so that
plot commands like the following are supported::

Expand All@@ -171,6 +170,14 @@ def __init__(self, axes, command='plot'):
self.command = command
self.set_color_cycle()

def __getstate__(self):
# note: it is not possible to pickle a itertools.cycle instance
return {'axes': self.axes, 'command': self.command}

def __setstate__(self, state):
self.__dict__ = state.copy()
self.set_color_cycle()

def set_color_cycle(self, clist=None):
if clist is None:
clist = rcParams['axes.color_cycle']
Expand DownExpand Up@@ -354,6 +361,7 @@ class Axes(martist.Artist):

def __str__(self):
return "Axes(%g,%g;%gx%g)" % tuple(self._position.bounds)

def __init__(self, fig, rect,
axisbg = None, # defaults to rc axes.facecolor
frameon = True,
Expand DownExpand Up@@ -488,6 +496,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
Expand DownExpand Up@@ -8815,7 +8832,15 @@ def __init__(self, fig, *args, **kwargs):
# _axes_class is set in the subplot_class_factory
self._axes_class.__init__(self, fig, self.figbox, **kwargs)


def __reduce__(self):
# get the first axes class which does not inherit from a subplotbase
axes_class = filter(lambda klass: (issubclass(klass, Axes) and
not issubclass(klass, SubplotBase)),
self.__class__.mro())[0]
r = [_PicklableSubplotClassConstructor(),
(axes_class,),
self.__getstate__()]
return tuple(r)

def get_geometry(self):
"""get the subplot geometry, eg 2,2,3"""
Expand DownExpand Up@@ -8897,6 +8922,22 @@ def subplot_class_factory(axes_class=None):
# This is provided for backward compatibility
Subplot = subplot_class_factory()


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 Pickling of Axes and Subplots.
"""
def __call__(self, axes_class):
# create a dummy object instance
subplot_instance = _PicklableSubplotClassConstructor()
subplot_class = subplot_class_factory(axes_class)
# update the class to the desired subplot class
subplot_instance.__class__ = subplot_class
return subplot_instance


docstring.interpd.update(Axes=martist.kwdoc(Axes))
docstring.interpd.update(Subplot=martist.kwdoc(Axes))

Expand Down
1 change: 0 additions & 1 deletionlib/matplotlib/axis.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -597,7 +597,6 @@ class Ticker:
formatter = None



class Axis(artist.Artist):

"""
Expand Down
2 changes: 1 addition & 1 deletionlib/matplotlib/backends/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
returnbackend_mod,new_figure_manager, draw_if_interactive, show


10 changes: 8 additions & 2 deletionslib/matplotlib/backends/backend_agg.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -385,7 +385,6 @@ def post_processing(image, dpi):
image)



def new_figure_manager(num, *args, **kwargs):
"""
Create a new figure manager instance
Expand All@@ -396,7 +395,14 @@ def new_figure_manager(num, *args, **kwargs):

FigureClass = kwargs.pop('FigureClass', Figure)
thisFig = FigureClass(*args, **kwargs)
canvas = FigureCanvasAgg(thisFig)
return new_figure_manager_given_figure(num, thisFig)


def new_figure_manager_given_figure(num, figure):
"""
Create a new figure manager instance for the given figure.
"""
canvas = FigureCanvasAgg(figure)
manager = FigureManagerBase(canvas, num)
return manager

Expand Down
95 changes: 51 additions & 44 deletionslib/matplotlib/backends/backend_cairo.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,15 +26,15 @@
def _fn_name(): return sys._getframe(1).f_code.co_name

try:
import cairo
import cairo
except ImportError:
raise ImportError("Cairo backend requires that pycairo is installed.")
raise ImportError("Cairo backend requires that pycairo is installed.")

_version_required = (1,2,0)
if cairo.version_info < _version_required:
raise ImportError ("Pycairo %d.%d.%d is installed\n"
"Pycairo %d.%d.%d or later is required"
% (cairo.version_info + _version_required))
raise ImportError ("Pycairo %d.%d.%d is installed\n"
"Pycairo %d.%d.%d or later is required"
% (cairo.version_info + _version_required))
backend_version = cairo.version
del _version_required

Expand DownExpand Up@@ -183,27 +183,27 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))

if ismath:
self._draw_mathtext(gc, x, y, s, prop, angle)
self._draw_mathtext(gc, x, y, s, prop, angle)

else:
ctx = gc.ctx
ctx.new_path()
ctx.move_to (x, y)
ctx.select_font_face (prop.get_name(),
self.fontangles [prop.get_style()],
self.fontweights[prop.get_weight()])

size = prop.get_size_in_points() * self.dpi / 72.0

ctx.save()
if angle:
ctx.rotate (-angle * np.pi / 180)
ctx.set_font_size (size)
if sys.version_info[0] < 3:
ctx.show_text (s.encode("utf-8"))
else:
ctx.show_text (s)
ctx.restore()
ctx = gc.ctx
ctx.new_path()
ctx.move_to (x, y)
ctx.select_font_face (prop.get_name(),
self.fontangles [prop.get_style()],
self.fontweights[prop.get_weight()])
size = prop.get_size_in_points() * self.dpi / 72.0
ctx.save()
if angle:
ctx.rotate (-angle * np.pi / 180)
ctx.set_font_size (size)
if sys.version_info[0] < 3:
ctx.show_text (s.encode("utf-8"))
else:
ctx.show_text (s)
ctx.restore()

def _draw_mathtext(self, gc, x, y, s, prop, angle):
if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))
Expand All@@ -215,28 +215,28 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
ctx.save()
ctx.translate(x, y)
if angle:
ctx.rotate (-angle * np.pi / 180)
ctx.rotate (-angle * np.pi / 180)

for font, fontsize, s, ox, oy in glyphs:
ctx.new_path()
ctx.move_to(ox, oy)

fontProp = ttfFontProperty(font)
ctx.save()
ctx.select_font_face (fontProp.name,
self.fontangles [fontProp.style],
self.fontweights[fontProp.weight])

size = fontsize * self.dpi / 72.0
ctx.set_font_size(size)
ctx.show_text(s.encode("utf-8"))
ctx.restore()
ctx.new_path()
ctx.move_to(ox, oy)
fontProp = ttfFontProperty(font)
ctx.save()
ctx.select_font_face (fontProp.name,
self.fontangles [fontProp.style],
self.fontweights[fontProp.weight])
size = fontsize * self.dpi / 72.0
ctx.set_font_size(size)
ctx.show_text(s.encode("utf-8"))
ctx.restore()

for ox, oy, w, h in rects:
ctx.new_path()
ctx.rectangle (ox, oy, w, h)
ctx.set_source_rgb (0, 0, 0)
ctx.fill_preserve()
ctx.new_path()
ctx.rectangle (ox, oy, w, h)
ctx.set_source_rgb (0, 0, 0)
ctx.fill_preserve()

ctx.restore()

Expand DownExpand Up@@ -397,10 +397,17 @@ def new_figure_manager(num, *args, **kwargs): # called by backends/__init__.py
"""
Create a new figure manager instance
"""
if _debug: print('%s.%s()' % (self.__class__.__name__,_fn_name()))
if _debug: print('%s()' % (_fn_name()))
FigureClass = kwargs.pop('FigureClass', Figure)
thisFig = FigureClass(*args, **kwargs)
canvas = FigureCanvasCairo(thisFig)
return new_figure_manager_given_figure(num, thisFig)


def new_figure_manager_given_figure(num, figure):
"""
Create a new figure manager instance for the given figure.
"""
canvas = FigureCanvasCairo(figure)
manager = FigureManagerBase(canvas, num)
return manager

Expand Down
11 changes: 10 additions & 1 deletionlib/matplotlib/backends/backend_cocoaagg.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,12 +35,21 @@

mplBundle = NSBundle.bundleWithPath_(os.path.dirname(__file__))


def new_figure_manager(num, *args, **kwargs):
FigureClass = kwargs.pop('FigureClass', Figure)
thisFig = FigureClass( *args, **kwargs )
canvas = FigureCanvasCocoaAgg(thisFig)
return new_figure_manager_given_figure(num, thisFig)


def new_figure_manager_given_figure(num, figure):
"""
Create a new figure manager instance for the given figure.
"""
canvas = FigureCanvasCocoaAgg(figure)
return FigureManagerCocoaAgg(canvas, num)


## Below is the original show() function:
#def show():
# for manager in Gcf.get_all_fig_managers():
Expand Down
9 changes: 8 additions & 1 deletionlib/matplotlib/backends/backend_emf.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -688,7 +688,14 @@ def new_figure_manager(num, *args, **kwargs):
# main-level app (egg backend_gtk, backend_gtkagg) for pylab
FigureClass = kwargs.pop('FigureClass', Figure)
thisFig = FigureClass(*args, **kwargs)
canvas = FigureCanvasEMF(thisFig)
return new_figure_manager_given_figure(num, thisFig)


def new_figure_manager_given_figure(num, figure):
"""
Create a new figure manager instance for the given figure.
"""
canvas = FigureCanvasEMF(figure)
manager = FigureManagerEMF(canvas, num)
return manager

Expand Down
7 changes: 7 additions & 0 deletionslib/matplotlib/backends/backend_fltkagg.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,6 +78,13 @@ def new_figure_manager(num, *args, **kwargs):
"""
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.
"""
window = Fltk.Fl_Double_Window(10,10,30,30)
canvas = FigureCanvasFltkAgg(figure)
window.end()
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp