我试图将用matplotlib创建的图形保存为pickle(不确定其他文件格式是否可以使用)文件,以便我可以重新打开图形并根据需要进行进一步的修改。但是,我想在保存之前查看该图。我发现类似的查询,例如: 1. Save python matplotlib figure as pickle, 2. Save plot to image file instead of displaying it using Matplotlib, 3. python matplotlib save graph as data file, 4. Saving interactive Matplotlib figures。
但是,他们中没有一个人在保存之前或谈到它之前都调用show()。
代码如下:
import matplotlib.pyplot as plt
import pickle
fig, ax = plt.subplots()
ax.plot(list(range(1, 100, 10)), 'bo-')
plt.show() # enabling this does not save anything with pickle
# save the mpl figure as pickle format
with open('fig1.pkl', 'wb') as fs:
pickle.dump(ax, fs)
plt.close("all")
# load the saved figure as mpl figure
with open('fig1.pkl', 'rb') as fl:
fig1 = pickle.load(fl)
plt.show()
如果我在第一种情况下不调用show(),它就可以正常工作。但是,如果我打电话给我,省钱似乎不适合泡菜。使用其他格式(.png / .pdf)保存可以正常工作。如果我有任何错误的任何迹象,将不胜感激。