我注意到,在将其保存为图像后,我无法腌制matplotlib图形,无论是png,jpeg还是pdf。为什么会这样?如果一个工作流程要求保存为png,然后再将其作为一个pickle,是否有一个干净的方法呢? 我正在使用matplotlib 1.3.1版,这是目前新版Mac OS(2016年初)的默认设置。
下面是一个简单的工作示例:
import matplotlib.pyplot as plt
import pickle
import numpy as np
eigval = np.random.rand(10)
nbins = 15
fig = plt.figure(figsize = (10*0.6, 5*0.6))
hist_vals = np.array([np.abs(np.imag(eigval[i])) for i in range(len(eigval))])
n, bins, patches = plt.hist(hist_vals, nbins)
plt.savefig('./test.png')
pickle.dump(fig, file('./test.pkl','w'))
返回
pickle.PicklingError: Can't pickle <built-in method copy_from_bbox of tuple object at 0x10bc1f998>: it's not found as __main__.copy_from_bbox
如果我们首先输出到pdf,我们得到
TypeError: can't pickle instancemethod objects
对于jpg,我们得到了
pickle.PicklingError: Can't pickle <type '_macosx.GraphicsContext'>: it's not found as _macosx.GraphicsContext
谢谢!