python - How to save an animation as gif and show it at the same time? -
i wrote code generates , shows matplotlib
animation. able save animation gif.
however when these 2 actions sequentially (first saving gif, , showing animation, because plt.show()
blocking call, shown animation doesn't start beginning; seems it's first used gif saving routine.
my idea show animation in window using plt.show()
, , save same animation gif disk (before, or ideally during animation). i've tried solve using threads or creating new figure, wasn't able pull off - shown animation modified saving routine. also, putting plt.show
thread didn't work (because handles drawing screen, suppose, needs in main thread).
here's code i'm using:
import matplotlib.pyplot plt matplotlib import animation visualizer import visualize import updater def run(k, update_func=updater.uniform, steps=1000, vis_steps=50, alpha_init=0.3, alpha_const=100, alpha_speed=0.95, diameter_init=3, diameter_const=1, diameter_speed=1, xlim=(-1, 1), ylim=(-1, 1), points_style='ro', lines_style='b-', save_as_gif=false): fig = plt.figure() ax = fig.add_subplot(111, xlim=xlim, ylim=ylim) global curr_alpha, curr_diameter, points, lines points, = ax.plot([], [], points_style) lines, = ax.plot([], [], lines_style) curr_alpha = alpha_init curr_diameter = diameter_init def update_attributes(i): global curr_alpha, curr_diameter if % alpha_const == 0: curr_alpha *= alpha_speed if % diameter_const == 0: curr_diameter *= diameter_speed def init(): lines.set_data([], []) points.set_data([], []) def loop(i): j in range(vis_steps): update_func(k, curr_alpha=curr_alpha, curr_diameter=curr_diameter) update_attributes(i * vis_steps + j) visualize(k, points, lines) return points, lines anim = animation.funcanimation(fig, loop, init_func=init, frames=steps // vis_steps, interval=1, repeat=false) if save_as_gif: anim.save('gifs/%s.gif' % save_as_gif, writer='imagemagick') plt.show()
Comments
Post a Comment