Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Qt4后端的matplotlib在缓存的呈现程序中出现问题_Python_Matplotlib_Qt4 - Fatal编程技术网

Python 使用Qt4后端的matplotlib在缓存的呈现程序中出现问题

Python 使用Qt4后端的matplotlib在缓存的呈现程序中出现问题,python,matplotlib,qt4,Python,Matplotlib,Qt4,我尝试使用matplotlib以更快的方式重新打印图像,因此我使用AxeImage类的set_data方法,而不是重新打印所有内容,如下所示: import numpy as np import time import matplotlib.pyplot as plt fig, ax = plt.subplots() img = plt.imshow(np.random.rand(100, 100)) img.set_data(np.random.rand(100, 100)) ax.dra

我尝试使用matplotlib以更快的方式重新打印图像,因此我使用AxeImage类的set_data方法,而不是重新打印所有内容,如下所示:

import numpy as np
import time
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
img = plt.imshow(np.random.rand(100, 100))

img.set_data(np.random.rand(100, 100))
ax.draw_artist(ax.patch)
ax.draw_artist(img)
fig.canvas.update()
fig.canvas.flush_events()
我遇到了以下错误:

回溯(最近一次呼叫上次):。。。 ax.draw\u artist(ax.patch)文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site packages/matplotlib/axes/_base.py”, 第2319行,在draw_艺术家中 raise AttributeError(msg)AttributeError:draw\u只能在缓存渲染的初始绘制之后使用

但是,如果我在pythonshell(IPython)中逐行运行脚本,它就可以正常工作,没有任何问题。那么,这个缓存渲染器背后的奥秘是什么呢


编辑:添加一条线
fig.canvas.draw()
解决了这个问题,现在剩下的问题是为什么在IPython shell中逐行运行它不会导致相同的错误?

正如您所想,为了缓存渲染器,必须至少绘制一次画布,然后才能调用:

绘画艺术家(a)

此方法只能在缓存渲染器的初始绘制之后使用。它用于有效更新轴数据(不更新轴标记、标签等)


我猜在IPython会话中,您正在运行matplotlib,在这种情况下,对
plt.subPlot
的初始调用将立即导致绘制新图形的画布并缓存渲染器

要复制脚本中看到的
AttributeError
,可以使用
plt.ioff()
关闭交互模式:

[1]中的
:plt.ioff()
[2]中:图,ax=plt.子图(1,1)
在[3]中:img=plt.imshow(np.random.rand(100100))
[4]中:img.set_数据(np.random.rand(100100))
在[5]:斧头画艺术家(斧头补丁)
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1个斧头画师(斧头补丁)
/home/alistair/.venvs/core/local/lib/python2.7/site-packages/matplotlib/axes//\u base.pyc in draw\u artist(self,a)
2317 msg=(“draw_美术师只能在初始绘制后使用,该绘制”
2318'缓存渲染')
->2319提高属性错误(msg)
2320 a.绘制(自绘制)
2321
AttributeError:draw\u Artister只能在缓存渲染的初始绘制之后使用

您还应该研究blit。@tcaswell根据上的帖子,blit会导致内存泄漏……这是一篇非常古老的文章。它可能已经被修复,如果没有,应该作为bug报告上游。
In [1]: plt.ioff()

In [2]: fig, ax = plt.subplots(1, 1)

In [3]: img = plt.imshow(np.random.rand(100, 100))

In [4]: img.set_data(np.random.rand(100, 100))

In [5]: ax.draw_artist(ax.patch)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-b54815e41caa> in <module>()
----> 1 ax.draw_artist(ax.patch)

/home/alistair/.venvs/core/local/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in draw_artist(self, a)
   2317             msg = ('draw_artist can only be used after an initial draw which'
   2318                    ' caches the render')
-> 2319             raise AttributeError(msg)
   2320         a.draw(self._cachedRenderer)
   2321 

AttributeError: draw_artist can only be used after an initial draw which caches the render