0

For example, this code works fine and saves the plt only when the plt.show() is present at the end. or else it just runs without saving any output.

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 - I tried using ani.save(writer="ffmpeg",dpi=200) and it works fine, but later on as I am using ffmpeg commands to convert the image sequence to animation, it shows invalid PNG signature.

Also I am new in this field so my appoligies if i did not follow any proper method.

Thanks in advance.

Suraj
  • 1
  • You can comment out `plt.savefig(str(i)+".png")` when you are saving using `ffpmeg` i.e. maybe `gif or mp4` with `ani.save` and note that `plt.show()` should not be a concern if you remove it, still `plt.savefig(str(i)+".png")` would work – Karthik Sep 12 '20 at 11:58
  • Removing `plt.show()` doesn't save the png files, I have tried. And as for `ani.save `, it works fine but I need an image sequence for conversion to animation via ffmpeg at a later stage. – Suraj Sep 12 '20 at 12:59

0 Answers0