I'm working on a script that animates the process of drawing a shape and saves the animation to a GIF. However, when I call plt.show()
after saving the animation, the popup window only displays the last frame of the animation. Here's the current code (hex_draw is just the module providing the drawing functions I use):
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation,PillowWriter
import hex_draw as hdraw
import json
def animate(frame,x_vals,y_vals,scale):
plt.plot(x_vals[frame],y_vals[frame],'go',ms=1.5*scale)
def main():
ax = plt.figure(figsize=(4,4)).add_axes([0,0,1,1])
plt.gca().axis("off")
ax.set_aspect("equal")
plot_data = hdraw.convert_to_points("qaq","east",settings)
hdraw.plot_monochrome(plot_data,settings)
ani = FuncAnimation(plt.gcf(),
func=animate,
fargs=[*plot_data[:3]],
frames=len(plot_data[0]),
interval=800,
repeat=False)
ani.save("test_anim.gif",dpi=100,writer=PillowWriter(fps=1))
plt.show()
with open("settings.json",mode="r") as file:
settings = json.load(file)
main()
Removing the line that saves the animation to a GIF causes the popup to animate properly, so that's definitely the cause of the problem. I've tried setting repeat
to True in the FuncAnimation call, and that doesn't fix anything.