Animations are created in 3 steps:

  • Initialize an Animation object.
  • Save each frame of the animation with frame(anim).
  • Convert the frames to an animated gif with gif(anim, filename, fps=15)
Tip: the convenience macros `@gif` and `@animate` simplify this code immensely. See the home page for examples of the short version, or the pyplot example for the long version.

Convenience macros

There are two macros for varying levels of convenience in creating animations: @animate and @gif. The main difference is that @animate will return an Animation object for later processing, and @gif will create an animated gif file (and display it when returned to an IJulia cell).

Use @gif for simple, one-off animations that you want to view immediately. Use @animate for anything more complex. Constructing Animation objects can be done when you need full control of the life-cycle of the animation (usually unnecessary though).


anim = @animate for i=1:100
gif(anim, "/tmp/anim_fps15.gif", fps = 15)
gif(anim, "/tmp/anim_fps30.gif", fps = 30)

The every flag will only save a frame "every N iterations":

@gif for i=1:100
end every 10

The when flag will only save a frame "when the expression is true"

@gif for i=1:100
end when i > 50 && mod1(i, 10) == 5

Custom Iterators

The newly added animate method allows you to pass an arbitrary iterator which returns the "input data" for plotting each frame of an animation. As an example, we'll use Iterators.repeatedly to give us an iterator which returns a random vector on each iteration:

using Plots, Iterators
itr = repeatedly(()->rand(10), 20)
animate(itr, ylims=(0,1), c=:red, fps=5)