我正在尝试为每个群集创建多个图像的子图,我有一个包含多个页面的pdf,每个群集一个。
我收到以下错误:
File "sparsity_diagnosis.py", line 382, in <module>
pdf.savefig(fig)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 2473, in savefig
figure.savefig(self, format='pdf', **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1565, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2232, in print_figure
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 2540, in print_pdf
file.endStream()
AttributeError: 'NoneType' object has no attribute 'endStream'
这是我的代码:
import numpy as np
import cPickle as pickle
import h5py
import pdb
from sklearn.cluster import KMeans
from PIL import Image
import matplotlib
import random
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.backends.backend_pdf
pdf = matplotlib.backends.backend_pdf.PdfPages("cow.pdf")
with open('cow pickle', "rb") as f:
cluster, example =pickle.load(f)
for i in range(64):
patches = example[str(i)]
samp_size_100 = 100 if len(patches) >= 100 else len(patches)
my_randoms_100 = random.sample(xrange(samp_size_100), 10)
samp_size_500 = 500 if len(patches) >= 500 else len(patches)
my_randoms_500 = random.sample(xrange(samp_size_500), 10)
fig = plt.figure()
fig.set_canvas(plt.gcf().canvas)
plt.axis('off')
for idx in range(30):
ax = fig.add_subplot(3, 10, idx+1)
if idx <10:
img = patches[idx]
elif idx < 20:
img = patches[my_randoms_100[idx-10]]
else:
img = patches[my_randoms_500[idx-20]]
img = np.transpose(img, (2,1,0))
ax.imshow(img)
pdf.savefig(fig)
pdf.close()
任何线索?