gpt4 book ai didi

python - MatPlotlib 中的 FPS 较低

转载 作者:行者123 更新时间:2023-12-01 00:09:49 27 4
gpt4 key购买 nike

面对这样的事实,MatPlotlib 在使用 self.frame.canvas.draw() 时,我在一个简单的图表上仅获得了 12 FPS。我发现一篇关于加速MatPlotlib的好文章:https://bastibe.de/2013-05-30-speeding-up-matplotlib.html但部分示例是工作代码,但代码最快 (500 FPS) 的示例无法工作。尝试阅读 MatPlotlib 文档尚未成功理解代码中的错误所在:“AttributeError:draw_artist 只能在缓存渲染器的初始绘制之后使用”。代码哪里出错了?

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 后端,您确实可以按照错误提示执行操作,即在开始循环之前绘制一次 Canvas 。

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 的更新,这样它就可以与任何后端一起使用,

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)

关于python - MatPlotlib 中的 FPS 较低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59702177/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com