我在一个psudo-operational环境中工作,我们在接收数据时创建新的图像.有时,当新数据进入时,我们需要重新打开图像并更新该图像以创建合成图像,添加叠加层等.除了添加图像之外,还需要修改标题,图例等.
matplotlib中是否有内容可以让我存储和重新加载我的matplotlib.pyplot对象供以后使用?它需要保持对所有相关对象的访问,包括数字,线条,图例等.也许泡菜是我正在寻找的,但我对此表示怀疑.
是否有可能在matplotlib中重新打开一个封闭的数字(即用户X'd)?以下代码显示了天真的方法:
In [14]: fig = figure(10)
In [15]: close(fig)
In [16]: fig.show()
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 495, in callit
func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 253, in idle_draw
self.draw()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 239, in draw
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/tkagg.py", line 19, in blit
tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError: this isn't a Tk application
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个带有小部件的图形,因此可以使用一种解决方法(只需创建一个新图形),但我想知道该figure实例在关闭后是否完全没用.
我希望能够获得创建的图形的 figure_manager:例如,我可以使用 pyplot 界面使用:
from pylab import*
figure()
plot(arange(100))
mngr = get_current_fig_manager()
Run Code Online (Sandbox Code Playgroud)
但是,如果我有几个数字怎么办:
from pylab import *
fig0 = figure()
fig1 = figure()
plot(arange(100))
mngr = fig0.get_manager() #DOES NOT WORK - no such method as Figure.get_manager()
Run Code Online (Sandbox Code Playgroud)
但是,仔细搜索图形 API http://matplotlib.org/api/figure_api.html并没有用。我的 IDE 中的图形实例也没有自动完成,似乎没有任何方法/成员给我一个“管理器”。
那么我该如何做到这一点,一般来说,如果有一个 pyplot 方法,我需要在 OO 界面中使用它的模拟方法,我应该在哪里查看?
PS:无论如何,get_current_fig_manager() 返回什么样的对象?在调试器中,我得到:
type(get_current_fig_manager())
<type 'instance'>
Run Code Online (Sandbox Code Playgroud)
这听起来很神秘......
我曾经pickle转储matplotlib数字,如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'))
Run Code Online (Sandbox Code Playgroud)
下面是加载腌制图的代码片段-
import pickle
figx = pickle.load(open('fig.pickle', 'rb'))
figx.show()
Run Code Online (Sandbox Code Playgroud)
上面的代码显示以下错误-
AttributeError: 'NoneType' object has no attribute 'manager'
Figure.show works only for figures managed by pyplot, normally created by pyplot.figure().
Run Code Online (Sandbox Code Playgroud)
我在 Ubuntu 14.04 LTS 64 位操作系统上使用 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)
Run Code Online (Sandbox Code Playgroud)
PS:我的问题似乎类似于在 …