首页 > 解决方案 > matplotlib.animation 可以在将帧保存到文件时显示帧吗?

问题描述

当我执行animator.save()时,plt.show()在保存过程完成之前不显示绘图。我有一个 Python 代码,它使用动画库来显示通过网络接收的数据,我想记录已经显示的数据。有没有办法实现这种行为?

标签: matplotlibanimation

解决方案


您可以将图形直接保存在更新函数中:

fig, ax = plt.subplots()
l, = ax.plot(range(10),[0]*10,'r-')
ax.set_ylim(0,1)

def animate(i):
    y = np.random.random(size=(10,))
    l.set_ydata(y)
    fig.savefig(f'test_{i}.png')
    return l,

ani = animation.FuncAnimation(fig, animate, frames=10)

推荐阅读