0

例如,此代码工作正常,仅当 plt.show() 出现在末尾时才保存 plt。否则它只是运行而不保存任何输出。

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

def animate(i):
    line.set_ydata(np.sin(2*np.pi*i / 50)*np.sin(x))
    #fig.canvas.draw() not needed see comment by @tacaswell
    plt.savefig(str(i)+".png")
    return line,

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1,1)
x = np.linspace(0, 2*np.pi, 200)
line, = ax.plot(x, np.zeros_like(x))
plt.draw()

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=5, repeat=False)
plt.show()

PS - 我尝试使用ani.save(writer="ffmpeg",dpi=200)它,它工作正常,但后来我使用 ffmpeg 命令将图像序列转换为动画,它显示无效的 PNG 签名。

我也是这个领域的新手,所以如果我没有遵循任何适当的方法,我会很高兴。

提前致谢。

4

0 回答 0