Put GIF animation code into py script.
The animation script used in the KF chapter is general enough to be used by the entire book, so I removed it from the notebook and put it in gif_animate.py
This commit is contained in:
parent
5e3a14a33e
commit
b4e991b783
File diff suppressed because one or more lines are too long
Binary file not shown.
Before Width: | Height: | Size: 465 KiB After Width: | Height: | Size: 481 KiB |
49
code/gif_animate.py
Normal file
49
code/gif_animate.py
Normal file
@ -0,0 +1,49 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Sun Sep 14 12:41:24 2014
|
||||
|
||||
@author: rlabbe
|
||||
"""
|
||||
|
||||
from matplotlib import animation
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def animate(filename, func, frames, interval, figsize=(6.5, 6.5)):
|
||||
""" Creates an animated GIF of a matplotlib.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
filename : string
|
||||
name of the file. E.g 'foo.GIF' or '\home\monty\parrots\fjords.gif'
|
||||
|
||||
func : function
|
||||
function that will be called once per frame. Must have signature of
|
||||
def fun_name(frame_num)
|
||||
|
||||
frames : int
|
||||
number of frames to animate. The current frame number will be passed
|
||||
into func at each call.
|
||||
|
||||
interval : float
|
||||
Milliseconds to pause on each frame in the animation. E.g. 500 for half
|
||||
a second.
|
||||
|
||||
figsize : (float, float) optional
|
||||
size of the figure in inches. Defaults to 6.5" by 6.5"
|
||||
"""
|
||||
|
||||
|
||||
|
||||
def forward(frame):
|
||||
# I don't know if it is a bug or what, but FuncAnimation calls twice
|
||||
# with the first frame number. That breaks any animation that uses
|
||||
# the frame number in computations
|
||||
if forward.first:
|
||||
forward.first = False
|
||||
return
|
||||
func(frame)
|
||||
|
||||
fig = plt.figure(figsize=figsize)
|
||||
forward.first = True
|
||||
anim = animation.FuncAnimation(fig, forward, frames=frames, interval=interval)
|
||||
anim.save(filename, writer='imagemagick')
|
Loading…
Reference in New Issue
Block a user