尝试加载腌制的matplotlib图时发生AttributeError [英] AttributeError while trying to load the pickled matplotlib figure
问题描述
我使用 pickle
转储 matplotlib
图,如 一个答案这样.下面是代码片段-
I used pickle
to dump matplotlib
figure as shown in an answer in SO. Below is the code snippet-
import pickle
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3],[10,-10,30])
pickle.dump(fig, open('fig.pickle', 'wb'))
下面是加载腌制图形的代码段-
Below is the code snippet to load the pickled figure-
import pickle
figx = pickle.load(open('fig.pickle', 'rb'))
figx.show()
上面的代码显示以下错误-
The above code shows following error-
AttributeError: 'NoneType' object has no attribute 'manager'
Figure.show works only for figures managed by pyplot, normally created by pyplot.figure().
我在 Ubuntu 14.04 LTS 64 位操作系统上使用 Python 3.6.3.以下是我的环境的更多详细信息-
I am using Python 3.6.3 on Ubuntu 14.04 LTS 64 Bit OS. Below are the more details of my environment-
> import matplotlib
> matplotlib.__version__
'2.1.0'
> matplotlib.get_backend()
'Qt5Agg'
> import sys
> sys.version_info
sys.version_info(major=3, minor=6, micro=3, releaselevel='final', serial=0)
PS:我的问题似乎与 SO提出的问题类似.然而,它是不同的,因为提供的答案没有运行并抛出异常.
PS: My questions seem similar to this question asked at SO. However, it is different since the provided answer is not running and throwing exceptions.
推荐答案
在显示图形之前,您需要一个画布管理器.问题相同的概念 Matplotlib: how要显示已关闭的图形适用,您可以创建一个函数来制作虚拟图形并窃取其经理,如下所示(归功于写上述答案的 Jean-Sébastien).
You need a canvas manager before you can show your figure. The same concept from question Matplotlib: how to show a figure that has been closed applies, you can create a function to make a dummy figure and steal its manager, as below (credit to Jean-Sébastien who wrote the answer above).
def show_figure(fig):
# create a dummy figure and use its
# manager to display "fig"
dummy = plt.figure()
new_manager = dummy.canvas.manager
new_manager.canvas.figure = fig
fig.set_canvas(new_manager.canvas)
使用此功能,您可以运行:
With this function you can then run:
show_figure(figx)
figx.show()
这篇关于尝试加载腌制的matplotlib图时发生AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!