Animated plots

EXAMPLES:

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])
sage: a.show() # optional -- ImageMagick
class sage.plot.animate.Animation(v, **kwds)

Bases: sage.structure.sage_object.SageObject

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 axes.
  • **kwds - all additional inputs are passed onto the rendering command. E.g., use figsize to adjust the resolution and aspect ratio.

EXAMPLES:

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

The show function takes arguments to specify the delay between frames (measured in hundredths of a second, default value 20) and the number of iterations (default value 0, which means to iterate forever). To iterate 4 times with half a second between each frame:

sage: a.show(delay=50, iterations=4) # optional -- ImageMagick

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() # optional -- ImageMagick
sage: show(L)

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

sage: animate([plot(sin, -1,1)], xmin=0, ymin=0)._kwds['xmin']
0

We check that Trac #7981 is fixed:

sage: a = animate([plot(sin(x + float(k)), (0, 2*pi), ymin=-5, ymax=5)
...            for k in srange(0,2*pi,0.3)])
sage: a.show() # optional -- ImageMagick
ffmpeg(savefile=None, show_path=False, output_format=None, ffmpeg_options='', delay=None, iterations=0, verbose=False)

Returns a movie showing an animation composed from rendering the graphics objects in self.

This function will only work if ffmpeg is installed. See http://www.ffmpeg.org for information about ffmpeg.

INPUT:

  • savefile - file that the mpeg gets saved to.
  • show_path - boolean (default: False); if True, print the path to the saved file
  • output_format - string (default: None); format and suffix to use for the video. This may be ‘mpg’, ‘mpeg’, ‘avi’, ‘gif’, or any other format that ffmpeg can handle. If this is None and the user specifies savefile with a suffix, say ‘savefile=animation.avi’, try to determine the format (‘avi’ in this case) from that file name. If no file is specified or if the suffix cannot be determined, ‘mpg’ is used.
  • ffmpeg_options - string (default: ‘’); this string is passed directly to ffmpeg.
  • delay - integer (default: None) delay in hundredths of a second between frames; i.e., the framerate is 100/delay. This is not supported for mpeg files: for mpegs, the frame rate is always 25 fps.
  • iterations - integer (default: 0); number of iterations of animation. If 0, loop forever. This is only supported for animated gif output.
  • verbose - boolean (default: False); if True, print messages produced by the ffmpeg command.

If savefile is not specified: in notebook mode, display the animation; otherwise, save it to a default file name.

EXAMPLES:

sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
...                xmin=0, xmax=2*pi, figsize=[2,1])
sage: dir = tmp_dir()
sage: a.ffmpeg(savefile=dir + 'new.mpg')       # optional -- ffmpeg
sage: a.ffmpeg(savefile=dir + 'new.avi')       # optional -- ffmpeg
sage: a.ffmpeg(savefile=dir + 'new.gif')       # optional -- ffmpeg
sage: a.ffmpeg(savefile=dir + 'new.mpg', show_path=True) # optional -- ffmpeg
Animation saved to .../new.mpg.

Note

If ffmpeg is not installed, you will get an error message like this:

Error: ffmpeg does not appear to be installed. Saving an animation to
a movie file in any format other than GIF requires this software, so
please install it and try again.

See www.ffmpeg.org for more information.

gif(delay=20, savefile=None, iterations=0, show_path=False, use_ffmpeg=False)

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

This function will only work if either (a) the ImageMagick software suite is installed, i.e., you have the convert command or (b) ffmpeg is installed. See www.imagemagick.org for more about ImageMagic, and see www.ffmpeg.org for more about ffmpeg. By default, this produces the gif using convert if it is present. If this can’t find convert or if use_ffmpeg is True, then it uses ffmpeg instead.

INPUT:

  • delay - (default: 20) delay in hundredths of a second between frames
  • savefile - file that the animated gif gets saved to
  • iterations - integer (default: 0); number of iterations of animation. If 0, loop forever.
  • show_path - boolean (default: False); if True, print the path to the saved file
  • use_ffmpeg - boolean (default: False); if True, use ‘ffmpeg’ by default instead of ‘convert’.

If savefile is not specified: in notebook mode, display the animation; otherwise, save it to a default file name.

EXAMPLES:

sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
...                xmin=0, xmax=2*pi, figsize=[2,1])
sage: dir = tmp_dir()
sage: a.gif()              # not tested
sage: a.gif(savefile=dir + 'my_animation.gif', delay=35, iterations=3)  # optional -- ImageMagick
sage: a.gif(savefile=dir + 'my_animation.gif', show_path=True) # optional -- ImageMagick
Animation saved to .../my_animation.gif.
sage: a.gif(savefile=dir + 'my_animation.gif', show_path=True, use_ffmpeg=True) # optional -- ffmpeg
Animation saved to .../my_animation.gif.

Note

If neither ffmpeg nor ImageMagick is installed, you will get an error message like this:

Error: Neither ImageMagick nor ffmpeg appears to be installed. Saving an
animation to a GIF file or displaying an animation requires one of these
packages, so please install one of them and try again.

See www.imagemagick.org and www.ffmpeg.org for more information.

AUTHORS:

  • William Stein
graphics_array(ncols=3)

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

EXAMPLES:

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)
sage: a
Animation with 4 frames
sage: a.show()        # optional -- ImageMagick
sage: g = a.graphics_array()
sage: print g
Graphics Array of size 1 x 3
sage: g.show(figsize=[4,1]) # optional
sage: g = a.graphics_array(ncols=2)
sage: print g
Graphics Array of size 2 x 2
sage: g.show('sage.png')         # optional
png(dir=None)

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

EXAMPLES:

sage: a = animate([plot(x^2 + n) for n in range(4)])
sage: d = a.png()
sage: v = os.listdir(d); v.sort(); v
['00000000.png', '00000001.png', '00000002.png', '00000003.png']
save(filename=None, show_path=False, use_ffmpeg=False)

Save this animation.

INPUT:

  • filename - (default: None) name of save file
  • show_path - boolean (default: False); if True, print the path to the saved file
  • use_ffmpeg - boolean (default: False); if True, use ‘ffmpeg’ by default instead of ‘convert’ when creating GIF files.

If filename is None, then in notebook mode, display the animation; otherwise, save the animation to a GIF file. If filename ends in ‘.sobj’, save to an sobj file. Otherwise, try to determine the format from the filename extension (‘.mpg’, ‘.gif’, ‘.avi’, etc.). If the format cannot be determined, default to gif.

For GIF files, either ffmpeg or the ImageMagick suite must be installed. For other movie formats, ffmpeg must be installed. An sobj file can be saved with no extra software installed.

EXAMPLES:

sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
...                xmin=0, xmax=2*pi, figsize=[2,1])
sage: dir = tmp_dir()
sage: a.save()         # not tested
sage: a.save(dir + 'wave.gif')   # optional -- ImageMagick
sage: a.save(dir + 'wave.gif', show_path=True)   # optional -- ImageMagick
Animation saved to file .../wave.gif.
sage: a.save(dir + 'wave.avi', show_path=True)   # optional -- ffmpeg
Animation saved to file .../wave.avi.
sage: a.save(dir + 'wave0.sobj')
sage: a.save(dir + 'wave1.sobj', show_path=True)
Animation saved to file .../wave1.sobj.
show(delay=20, iterations=0)

Show this animation.

INPUT:

  • delay - (default: 20) delay in hundredths of a second between frames
  • iterations - integer (default: 0); number of iterations of animation. If 0, loop forever.

Note

Currently this is done using an animated gif, though this could change in the future. This requires that either ffmpeg or the ImageMagick suite (in particular, the convert command) is installed.

See also the ffmpeg() method.

EXAMPLES:

sage: a = animate([sin(x + float(k)) for k in srange(0,2*pi,0.7)],
...                xmin=0, xmax=2*pi, figsize=[2,1])
sage: a.show()       # optional -- ImageMagick

The preceding will loop the animation forever. If you want to show only three iterations instead:

sage: a.show(iterations=3)    # optional -- ImageMagick

To put a half-second delay between frames:

sage: a.show(delay=50)        # optional -- ImageMagick

Note

If you don’t have ffmpeg or ImageMagick installed, you will get an error message like this:

Error: Neither ImageMagick nor ffmpeg appears to be installed. Saving an
animation to a GIF file or displaying an animation requires one of these
packages, so please install one of them and try again.

See www.imagemagick.org and www.ffmpeg.org for more information.

Previous topic

Graphics objects

Next topic

Arcs of circles and ellipses

This Page