In [1]:
# This notebook requires pylab
# e.g., start the notebook server with --oylab=inline, or run
# from pylab import *
# create the folder to store the images
!mkdir -p anim
!rm anim/*.png
fig = figure(figsize=(5,5))
ax = fig.add_subplot(1, 1, 1)
t = linspace(0, 2.*pi, 100)
x = cos(t)
y = sin(t)
ax.plot(x,y)
phi = 0
r = plot(cos(phi), sin(phi), 'rd', markersize=12 )
for imnum, phi in enumerate(linspace(0,2.*pi, 100)):
r[0].set_data(cos(phi), sin(phi)) # update the line properties
# note that only the final figure will be transferred to the browser!
draw() # necessary to update the figure!
fig.savefig('anim/fig%03i.png' % imnum, res=80) # resolution: 80dpi
In [2]:
# convert into mp4:
# old version (raises deprecation warning
#!ffmpeg -f image2 -i anim/fig%03d.png anim.mp4
# new version
# -y: overwrite existing file, -r 20: 20 fps, -f image2 : filetype image,
# -i anim/fig%03d.png: input files, all that start with anim/fig, have 3 numbers, and end with .png
# anim/anim.ogg - the output file name
# *NOTE* 'ogg' is web-compatible. If you want another format, just change the suffix (e.g. '.mp4')
!avconv -y -r 20 -f image2 -i anim/fig%03d.png anim/anim.ogg
#!rm anim/*.png
In [3]:
# display the video inline!
# note: this needs a recent browser... with HTML5 and the option of OGG-video playback
from IPython.core.display import HTML
video = open('anim/anim.ogg', 'rb').read()
video_encoded = video.encode('base64') # encode the video in HTML-conform style
video_tag = ('<video width="400" height="400" controls type="video/ogg" '
+ 'alt="our simple animation" src="data:video/ogg;base64,{0}">').format(video_encoded)
HTML(data = video_tag)
Out[3]:
In [33]: