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

Commit97eb612

Browse files
committed
Merge pull request#3627 from pelson/pickle_bug
BUG : Fixed Image and Renderer pickling
2 parents68b34b0 +4ba18d4 commit97eb612

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

‎lib/matplotlib/backends/backend_agg.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ def __init__(self, width, height, dpi):
104104
if__debug__:verbose.report('RendererAgg.__init__ done',
105105
'debug-annoying')
106106

107+
def__getstate__(self):
108+
# We only want to preserve the init keywords of the Renderer.
109+
# Anything else can be re-created.
110+
return {'width':self.width,'height':self.height,'dpi':self.dpi}
111+
112+
def__setstate__(self,state):
113+
self.__init__(state['width'],state['height'],state['dpi'])
114+
107115
def_get_hinting_flag(self):
108116
ifrcParams['text.hinting']:
109117
returnLOAD_FORCE_AUTOHINT

‎lib/matplotlib/image.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111
importos
1212
importwarnings
13-
importmath
1413

1514
importnumpyasnp
16-
fromnumpyimportma
1715

1816
frommatplotlibimportrcParams
1917
importmatplotlib.artistasmartist
@@ -113,6 +111,12 @@ def __init__(self, ax,
113111

114112
self.update(kwargs)
115113

114+
def__getstate__(self):
115+
state=super(_AxesImageBase,self).__getstate__()
116+
# We can't pickle the C Image cached object.
117+
state.pop('_imcache',None)
118+
returnstate
119+
116120
defget_size(self):
117121
"""Get the numrows, numcols of the input image"""
118122
ifself._AisNone:

‎lib/matplotlib/lines.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ def __init__(self, xdata, ydata,
352352
self._invalidy=True
353353
self.set_data(xdata,ydata)
354354

355+
def__getstate__(self):
356+
state=super(Line2D,self).__getstate__()
357+
# _linefunc will be restored on draw time.
358+
state.pop('_lineFunc',None)
359+
returnstate
360+
355361
defcontains(self,mouseevent):
356362
"""
357363
Test whether the mouse event occurred on the line. The pick

‎lib/matplotlib/tests/test_pickle.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def test_simple():
107107
plt.plot(list(xrange(10)),label='foobar')
108108
plt.legend()
109109

110+
# Uncomment to debug any unpicklable objects. This is slow so is not
111+
# uncommented by default.
110112
# recursive_pickle(fig)
111113
pickle.dump(ax,BytesIO(),pickle.HIGHEST_PROTOCOL)
112114

@@ -195,17 +197,47 @@ def test_complete():
195197

196198
deftest_no_pyplot():
197199
# tests pickle-ability of a figure not created with pyplot
198-
199-
importpickleasp
200200
frommatplotlib.backends.backend_pdfimportFigureCanvasPdfasfc
201201
frommatplotlib.figureimportFigure
202202

203203
fig=Figure()
204-
can=fc(fig)
204+
_=fc(fig)
205205
ax=fig.add_subplot(1,1,1)
206206
ax.plot([1,2,3], [1,2,3])
207-
208-
# Uncomment to debug any unpicklable objects. This is slow so is not
209-
# uncommented by default.
210-
# recursive_pickle(fig)
211207
pickle.dump(fig,BytesIO(),pickle.HIGHEST_PROTOCOL)
208+
209+
210+
deftest_renderer():
211+
frommatplotlib.backends.backend_aggimportRendererAgg
212+
renderer=RendererAgg(10,20,30)
213+
pickle.dump(renderer,BytesIO())
214+
215+
216+
deftest_image():
217+
# Prior to v1.4.0 the Image would cache data which was not picklable
218+
# once it had been drawn.
219+
frommatplotlib.backends.backend_aggimportnew_figure_manager
220+
manager=new_figure_manager(1000)
221+
fig=manager.canvas.figure
222+
ax=fig.add_subplot(1,1,1)
223+
ax.imshow(np.arange(12).reshape(3,4))
224+
manager.canvas.draw()
225+
pickle.dump(fig,BytesIO())
226+
227+
228+
deftest_grid():
229+
frommatplotlib.backends.backend_aggimportnew_figure_manager
230+
manager=new_figure_manager(1000)
231+
fig=manager.canvas.figure
232+
ax=fig.add_subplot(1,1,1)
233+
ax.grid()
234+
# Drawing the grid triggers instance methods to be attached
235+
# to the Line2D object (_lineFunc).
236+
manager.canvas.draw()
237+
238+
pickle.dump(ax,BytesIO())
239+
240+
241+
if__name__=='__main__':
242+
importnose
243+
nose.runmodule(argv=['-s'])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp