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

Commitbd0df1f

Browse files
committed
Backport PR#23462: Fix AttributeError for pickle load of Figure class
There were a number of complications with the backport: - the new test generalized an existing test that had picked up additional changes on the main branch - the signature of the subproccess helper has changed on the main branch
1 parent4ee298b commitbd0df1f

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

‎lib/matplotlib/figure.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2919,7 +2919,8 @@ def __setstate__(self, state):
29192919
importmatplotlib._pylab_helpersaspylab_helpers
29202920
allnums=plt.get_fignums()
29212921
num=max(allnums)+1ifallnumselse1
2922-
mgr=plt._backend_mod.new_figure_manager_given_figure(num,self)
2922+
backend=plt._get_backend_mod()
2923+
mgr=backend.new_figure_manager_given_figure(num,self)
29232924
pylab_helpers.Gcf._set_new_active_manager(mgr)
29242925
plt.draw_if_interactive()
29252926

‎lib/matplotlib/tests/test_pickle.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
fromioimportBytesIO
2+
importast
23
importpickle
34

45
importnumpyasnp
56
importpytest
67

78
frommatplotlibimportcm
89
frommatplotlib.testing.decoratorsimportimage_comparison
10+
importmatplotlibasmpl
11+
frommatplotlib.testingimportsubprocess_run_helper
12+
frommatplotlib.testing.decoratorsimportcheck_figures_equal
913
frommatplotlib.datesimportrrulewrapper
1014
importmatplotlib.pyplotasplt
1115
importmatplotlib.transformsasmtransforms
@@ -110,6 +114,103 @@ def test_complete():
110114
assertfig.get_label()=='Figure with a label?'
111115

112116

117+
def_pickle_load_subprocess():
118+
importos
119+
importpickle
120+
121+
path=os.environ['PICKLE_FILE_PATH']
122+
123+
withopen(path,'rb')asblob:
124+
fig=pickle.load(blob)
125+
126+
print(str(pickle.dumps(fig)))
127+
128+
129+
def_generate_complete_test_figure(fig_ref):
130+
fig_ref.set_size_inches((10,6))
131+
plt.figure(fig_ref)
132+
133+
plt.suptitle('Can you fit any more in a figure?')
134+
135+
# make some arbitrary data
136+
x,y=np.arange(8),np.arange(10)
137+
data=u=v=np.linspace(0,10,80).reshape(10,8)
138+
v=np.sin(v*-0.6)
139+
140+
# Ensure lists also pickle correctly.
141+
plt.subplot(3,3,1)
142+
plt.plot(list(range(10)))
143+
144+
plt.subplot(3,3,2)
145+
plt.contourf(data,hatches=['//','ooo'])
146+
plt.colorbar()
147+
148+
plt.subplot(3,3,3)
149+
plt.pcolormesh(data)
150+
151+
plt.subplot(3,3,4)
152+
plt.imshow(data)
153+
154+
plt.subplot(3,3,5)
155+
plt.pcolor(data)
156+
157+
ax=plt.subplot(3,3,6)
158+
ax.set_xlim(0,7)
159+
ax.set_ylim(0,9)
160+
plt.streamplot(x,y,u,v)
161+
162+
ax=plt.subplot(3,3,7)
163+
ax.set_xlim(0,7)
164+
ax.set_ylim(0,9)
165+
plt.quiver(x,y,u,v)
166+
167+
plt.subplot(3,3,8)
168+
plt.scatter(x,x**2,label='$x^2$')
169+
plt.legend(loc='upper left')
170+
171+
plt.subplot(3,3,9)
172+
plt.errorbar(x,x*-0.5,xerr=0.2,yerr=0.4)
173+
174+
175+
@mpl.style.context("default")
176+
@check_figures_equal(extensions=['png'])
177+
deftest_pickle_load_from_subprocess(fig_test,fig_ref,tmp_path):
178+
_generate_complete_test_figure(fig_ref)
179+
180+
fp=tmp_path/'sinus.pickle'
181+
assertnotfp.exists()
182+
183+
withfp.open('wb')asfile:
184+
pickle.dump(fig_ref,file,pickle.HIGHEST_PROTOCOL)
185+
assertfp.exists()
186+
187+
proc=subprocess_run_helper(
188+
_pickle_load_subprocess,
189+
timeout=60,
190+
PICKLE_FILE_PATH=str(fp),
191+
)
192+
193+
loaded_fig=pickle.loads(ast.literal_eval(proc.stdout))
194+
195+
loaded_fig.canvas.draw()
196+
197+
fig_test.set_size_inches(loaded_fig.get_size_inches())
198+
fig_test.figimage(loaded_fig.canvas.renderer.buffer_rgba())
199+
200+
plt.close(loaded_fig)
201+
202+
203+
deftest_gcf():
204+
fig=plt.figure("a label")
205+
buf=BytesIO()
206+
pickle.dump(fig,buf,pickle.HIGHEST_PROTOCOL)
207+
plt.close("all")
208+
assertplt._pylab_helpers.Gcf.figs== {}# No figures must be left.
209+
fig=pickle.loads(buf.getbuffer())
210+
assertplt._pylab_helpers.Gcf.figs!= {}# A manager is there again.
211+
assertfig.get_label()=="a label"
212+
213+
113214
deftest_no_pyplot():
114215
# tests pickle-ability of a figure not created with pyplot
115216
frommatplotlib.backends.backend_pdfimportFigureCanvasPdf

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp