python - 使用 Qt4 后端的 matplotlib 存在缓存渲染器问题

标签 python matplotlib qt4

我试图使用 matplotlib 以更快的方式重新绘制图像,因此我没有重新绘制所有内容,而是使用 AxesImage 类的 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()

我遇到了这个错误:

Traceback (most recent call last): ... ax.draw_artist(ax.patch) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/_base.py", line 2319, in draw_artist raise AttributeError(msg) AttributeError: draw_artist can only be used after an initial draw which caches the render

但是,如果我在 python shell (IPython) 中逐行运行脚本,它就会工作并且没有任何问题。那么这个缓存渲染器背后到底有何玄机呢?

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

最佳答案

正如您所想,在调用 draw_artist 之前,必须至少绘制一次 Canvas 才能缓存渲染器。 :

draw_artist(a)

This method can only be used after an initial draw which caches the renderer. It is used to efficiently update Axes data (axis ticks, labels, etc are not updated)


我猜测在您的 IPython session 中您正在 interactive mode 中运行 matplotlib ,在这种情况下,您对 plt.subplots 的初始调用将立即导致绘制新图形的 Canvas 并缓存渲染器。

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

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

关于python - 使用 Qt4 后端的 matplotlib 存在缓存渲染器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33984798/

相关文章:

python - 连接两个 Pandas .HDFStore HDF5文件

python - 使用 matplotlib 绘图时更改刻度标签的位置(移动)

python - 在图像中绘制标记

matplotlib - dpi与图形尺寸的关系

c++ - 在 QPlainTextEdit 中获取指向 QTextBlock 的指针

python - 如何更改标题数据

python - Python校准相机

python - numpy 和 ctypes : dealing with views

python - Nan 不会在 Python 中退出

c++ - 将 QImage 保存到 QBuffer