[试图加载腌制的matplotlib图时出现AttributeError

问题描述 投票:3回答:1

我用pickle转储了matplotlib图形,如an answer in SO所示。下面是代码片段-

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'))

下面是加载腌制图的代码段-

import pickle
figx = pickle.load(open('fig.pickle', 'rb'))
figx.show()

上面的代码显示以下错误-

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位OS上使用Python 3.6.3。以下是我的环境的更多详细信息-

> 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:我的问题似乎类似于this question asked at SO。但是,这是不同的,因为提供的答案没有运行并引发异常。

python matplotlib
1个回答
6
投票

显示图形之前,您需要一个画布管理器。适用问题Matplotlib: how to show a figure that has been closed的相同概念,您可以创建一个函数来制作虚拟角色并窃取其管理者,如下所示(版权归让·塞巴斯蒂安(Jean-Sébastien),他写了以上答案)。

© www.soinside.com 2019 - 2024. All rights reserved.