5.2 Animated plots

Module: sage.plot.animate

Animated plots

We plot a circle shooting up to the right:

sage: a = animate([circle((i,i), 1-1/(i+1), hue=i/10) for i in srange(0,2,0.2)], 
...               xmin=0,ymin=0,xmax=2,ymax=2,figsize=[2,2]) # optional --
requires convert command
sage: a.show() # optional -- requires convert command

Class: Animation

class Animation
Return an animation of a sequence of plots of objects.

Input:

v
- list of SAGE objects. These should preferably be graphics objects, but if they aren't then plot is called on them.
xmin, xmax, ymin, ymax
- the ranges of the x and y axis.
**kwds
- all additional inputs are passed onto the rendering command. E.g., use figsize to adjust the resolution and aspect ratio.

sage: a = animate([sin(x + float(k)) for k in srange(0,4,0.3)], 
...                xmin=0, xmax=2*pi, figsize=[2,1]) # optional -- requires
convert command
sage: a # optional
Animation with 14 frames
sage: a[:5] # optional
Animation with 5 frames
sage: a.show()          # optional
sage: a[:5].show()      # optional

We draw an animation of drawing a parabola:

sage: step = 0.1
sage: L = Graphics()
sage: v = []
sage: for i in srange(0,1,step):
...       L += line([(i,i^2),(i+step,(i+step)^2)], rgbcolor=(1,0,0),
thickness=2)
...       v.append(L)
...
sage: a = animate(v, xmin=0, ymin=0)
sage: a.show()
sage: show(L)

TESTS: This illustrates ticket #2066 is fixed (setting axes ranges when an endpoint is 0):

sage: animate(plot(sin, -1,1), xmin=0, ymin=0)._Animation__xmin
0
Animation( self, v, [xmin=None], [xmax=None], [ymin=None], [ymax=None])

Functions: gif,$ \,$ graphics_array,$ \,$ png,$ \,$ save,$ \,$ show

gif( self, [delay=20], [outfile=None], [iterations=0])

Returns an animated gif composed from rendering the graphics objects in self.

This function will only work if the Imagemagick command line tools package is installed, i.e., you have the ``convert'' command.

Input:

delay
- (default: 20) delay in hundredths of a second between frames
outfile
- file that the animated gif gets saved to
iterations
- integer (default: 0); number of iterations of animation. If 0, loop forever.

Author: William Stein

graphics_array( self, [ncols=3])

Return a graphics array with the given number of columns with plots of the frames of this animation.

sage: E = EllipticCurve('37a')
sage: v = [E.change_ring(GF(p)).plot(pointsize=30) for p in [97, 101, 103, 107]]
sage: a = animate(v, xmin=0, ymin=0) # optional -- requires convert command
sage: a # optional
Animation with 4 frames
sage: a.show()        # optional

sage: g = a.graphics_array() # optional
sage: print g # optional
Graphics Array of size 1 x 3
sage: g.show(figsize=[4,1]) # optional

sage: g = a.graphics_array(ncols=2) # optional
sage: print g # optional
Graphics Array of size 2 x 2
sage: g.show('sage.png')         # optional

png( self, [dir=None])

Return the absolute path to a temp directory that contains the rendered png's of all the images in this animation.

sage: a = animate([plot(x^2 + n) for n in range(4)]) # optional -- requires convert command
sage: d = a.png() # optional  -- directory where the files are
sage: v = os.listdir(d); v.sort(); v # optional
['00000000.png', '00000001.png', '00000002.png', '00000003.png']

show( self, [delay=20], [iterations=0])

Show this animation.

Currently this is done by default using an animated gif, though this could change in the future.

Special Functions: __add__,$ \,$ __getitem__,$ \,$ __getslice__,$ \,$ __init__,$ \,$ __mul__,$ \,$ _repr_,$ \,$ _set_axes

__add__( self, other)

Add two animations. This has the effect of superimposing the two animinations frame-by-frame.

We add and multiply two animations.

sage: a = animate([circle((i,0),1) for i in srange(0,2,0.4)], 
...                xmin=0, ymin=-1, xmax=3, ymax=1, figsize=[2,1]) #
optional -- requires convert command
sage: a.show()   # optional
sage: b = animate([circle((0,i),1,hue=0) for i in srange(0,2,0.4)], 
...                xmin=0, ymin=-1, xmax=1, ymax=3, figsize=[1,2]) #
optional
sage: b.show() # optional
sage: (a*b).show()    # optional
sage: (a+b).show()    # optional

__getitem__( self, i)

Get a frame from an animation.

sage: a = animate([x, x^2, x^3, x^4]) # optional -- requires convert command
sage: a[2].show()       # optional

__getslice__( self)

Slice this animation returning a subanimation.

sage: a = animate([circle((i,-i), 1-1/(i+1), hue=i/10) for i in srange(0,2,0.2)], 
...               xmin=0,ymin=-2,xmax=2,ymax=0,figsize=[2,2]) # optional --
requires convert command
...
sage: a # optional
Animation with 10 frames
sage: a.show() # optional
sage: a[3:7] # optional
Animation with 4 frames
sage: a[3:7].show() # optional

__mul__( self, other)

Multiply two animations. This has the effect of appending the two animinations (the second comes after the first).

We add and multiply two animations.

sage: a = animate([circle((i,0),1,thickness=20*i) for i in srange(0,2,0.4)], 
...                xmin=0, ymin=-1, xmax=3, ymax=1, figsize=[2,1],
axes=False) # optional -- requires convert command
sage: a.show()     # optional
sage: b = animate([circle((0,i),1,hue=0,thickness=20*i) for i in srange(0,2,0.4)], 
...                xmin=0, ymin=-1, xmax=1, ymax=3, figsize=[1,2],
axes=False) # optional
sage: b.show()             # optional
sage: (a*b).show()         # optional
sage: (a+b).show()         # optional

See About this document... for information on suggesting changes.