'Animation.save does not save all frames in matplotlib

I have the following code to animate plot:

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update, frames=generate(), fargs=(lines,),
                                   interval=5, blit=False)

#plt.show()
line_ani.save("animation.mp4")

generate function yields data and update function set this data onto the plot.

When I run this script with plt.show() I see very slow and long animation.

When I run this script with line_ani.save("animation.mp4") it ends very soon and produces very short animation inside mp4 file.

Why and how to fix?

UPDATE

Although script runs without any errors, PyCharms is reporting a warning:

enter image description here

Expected type 'Optional[int]', got 'Generator[...



Solution 1:[1]

I had the same issue - you can fix it by supplying the 'FuncAnimation' function with a 'save_count' argument, which tells the function the number of frames it should save (i.e., the number of frames you want to animate). I don't know why this argument is in the 'FuncAnimation' and not in the .save() method...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon