MatPlotlib中的FPS低 [英] Low FPS in MatPlotlib

查看:69
本文介绍了MatPlotlib中的FPS低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

面对MatPlotlib在使用self.frame.canvas.draw()时,在一个简单的图表上我只有12 FPS.我找到了一篇有关加速MatPlotlib的好文章: https://bastibe.de/2013-05-30-speeding-up-matplotlib.html 但部分示例是有效代码,但代码最快(500 FPS)的示例无效.尝试阅读MatPlotlib文档尚未成功理解代码中的错误位置:«AttributeError:draw_artist只能在缓存了渲染器的初始绘制之后使用».代码中的错误在哪里?

Faced with the fact that MatPlotlib when using self.frame.canvas.draw() I got only 12 FPS on a simple chart. I found a good article about acceleration speed MatPlotlib: https://bastibe.de/2013-05-30-speeding-up-matplotlib.html But part of the examples is working code, but the example with the fastest code (500 FPS) is not working. Attempts to read the documentation MatPlotlib have not yet led to success in understanding where the error is in the code: «AttributeError: draw_artist can only be used after an initial draw which caches the renderer». Where is the error in the code?

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

fig, ax = plt.subplots()
line, = ax.plot(np.random.randn(100))
plt.show(block=False)

tstart = time.time()
num_plots = 0
while time.time()-tstart < 5:
    line.set_ydata(np.random.randn(100))
    ax.draw_artist(ax.patch)
    ax.draw_artist(line)
    fig.canvas.update()
    fig.canvas.flush_events()
    num_plots += 1
print(num_plots/5)

推荐答案

如果使用 Qt5Agg 后端,您确实可以按照错误提示进行操作,即在开始循环之前绘制画布一次.

If using the Qt5Agg backend, you can indeed just do what the error suggest, namely draw the canvas once before starting the loop.

import time
import numpy as np
import matplotlib
matplotlib.use("Qt5Agg")
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
line, = ax.plot(np.random.randn(100))

fig.canvas.draw()
plt.show(block=False)


tstart = time.time()
num_plots = 0
while time.time()-tstart < 5:
    line.set_ydata(np.random.randn(100))
    ax.draw_artist(ax.patch)
    ax.draw_artist(line)
    fig.canvas.update()
    fig.canvas.flush_events()
    num_plots += 1
print(num_plots/5)

然而,我实际上会使用 blit 而不是 PyQt 的更新,这样它就可以与任何后端一起使用,

However, I would actually use blit instead of PyQt's update, such that it would work with any backend,

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

fig, ax = plt.subplots()
line, = ax.plot(np.random.randn(100))

fig.canvas.draw()
plt.show(block=False)


tstart = time.time()
num_plots = 0
while time.time()-tstart < 5:
    line.set_ydata(np.random.randn(100))
    ax.draw_artist(ax.patch)
    ax.draw_artist(line)
    fig.canvas.blit(ax.bbox)
    fig.canvas.flush_events()
    num_plots += 1
print(num_plots/5)

这篇关于MatPlotlib中的FPS低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆