我正在尝试使用以下代码片段保存动画:
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import copy
......
frames = []
for i in range(500):
config_copy = copy.deepcopy(config)
frames.append((plt.imshow(config_copy, cmap='Greys_r', aspect=1,
interpolation='none', vmin=-1, vmax=1), plt.scatter(loc[1], loc[0], c='red', marker='o', s=50)))
...
animation = FuncAnimation(f, lambda x: x, frames=frames, blit=True, interval=200)
animation.save('fun_animation_random_direction.gif', writer='imagemagick', fps=30)
plt.show()
当我运行代码时,动画会按预期显示!但是当我打开生成的 gif 文件时,我只看到一张似乎同时显示所有帧的图片。我该如何解决这个问题?
我尝试使用 Windows 照片查看器以外的其他应用程序打开 gif 文件,但没有成功...
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import copy
# Assuming config and loc are defined and updated within the loop
frames = []
for i in range(500):
# Assuming config and loc are updated within the loop
config_copy = copy.deepcopy(config) # Assuming config is updated in each iteration
frames.append((plt.imshow(config_copy, cmap='Greys_r', aspect=1,
interpolation='none', vmin=-1, vmax=1),
plt.scatter(loc[1], loc[0], c='red', marker='o', s=50)))
# Check if frames are correctly collected and if they represent the changes over time
# Show the animation before saving
fig, ax = plt.subplots()
ani = animation.ArtistAnimation(fig, frames, interval=200, blit=True)
plt.show()
# Save the animation using different parameters or writers
ani.save('fun_animation_random_direction.gif', writer='imagemagick', fps=30)