Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872# -*- coding: utf-8 -*-1"""2Created on Sun Sep 14 12:41:24 201434@author: rlabbe5"""67from matplotlib import animation8import matplotlib.pyplot as plt910def animate(filename, func, frames, interval, fig=None, figsize=(6.5, 6.5)):11""" Creates an animated GIF of a matplotlib.1213Parameters14----------15filename : string16name of the file. E.g 'foo.GIF' or '\home\monty\parrots\fjords.gif'1718func : function19function that will be called once per frame. Must have signature of20def fun_name(frame_num)2122frames : int23number of frames to animate. The current frame number will be passed24into func at each call.2526interval : float27Milliseconds to pause on each frame in the animation. E.g. 500 for half28a second.2930figsize : (float, float) optional31size of the figure in inches. Defaults to 6.5" by 6.5"32"""33343536def forward(frame):37# I don't know if it is a bug or what, but FuncAnimation calls twice38# with the first frame number. That breaks any animation that uses39# the frame number in computations40if forward.first:41forward.first = False42return43func(frame)4445if fig is None:46fig = plt.figure(figsize=figsize)47forward.first = True48anim = animation.FuncAnimation(fig, forward, frames=frames, interval=interval)49anim.save(filename, writer='imagemagick')5051