Animations (FuncAnimation)

FuncAnimation is the Matplotlib tool that turns a static plot into motion by calling an update function once per frame to redraw the figure step by step.

Learn Animations (FuncAnimation) in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll write update and init functions, control frames , interval , and blit , update line data with set_data , and learn how to save to a GIF or MP4.

The pattern is always the same: create a figure, make an artist (here an empty line), and write an update(frame) function that changes that artist using set_data() . Hand the figure, the function, the frame count, and the delay to FuncAnimation , and store the result in a variable so it stays alive.

What you'll see: a blank set of axes that progressively traces out a blue sine wave from left to right as the frame number climbs, finishing as a full single period of the curve.

For smoother playback, set blit=True so Matplotlib only redraws what changed. That requires your functions to return the artists they touched, and it pairs with an init_func that paints the clean starting state. Here the whole wave is redrawn each frame, shifted a little, to make it travel.

What you'll see: a full orange sine wave that glides steadily to the right, each frame shifting it a fraction further so it appears to flow across the plot.

You can animate any artist, not just full curves — here a single marker traces a circle by recomputing its position from the frame number. Once the animation looks right, export it with anim.save() : the Pillow writer makes a GIF, and the FFmpeg writer makes an MP4 (FFmpeg must be installed for that).

What you'll see: a single crimson dot that travels smoothly around a circular path, returning to its start after 60 frames. The commented save line shows how you would export it as a GIF.

Replace each ___ to update the line every frame and create the animation.

Hint: the method that swaps a line's data is set_data , and the constructor is FuncAnimation .

❌ The animation never plays or freezes instantly

You didn't keep a reference. Assign it: anim = FuncAnimation(...) , or it gets garbage-collected.

Your update must return the artists it changed, e.g. return (line,) . blit redraws only what is returned.

Pass sequences, not bare numbers: point.set_data([x], [y]) with one-element lists.

Animate four bars rising from zero to their target heights, updating each bar with set_height() every frame.

Lesson complete — your plots can move now!

You built animations with FuncAnimation , wrote update and init functions, redrew artists with set_data and set_height , tuned frames , interval , and blit , and saw how to save to GIF or MP4.

🚀 Up next: Checkpoint: Advanced Plotting — put every advanced skill together in one build.

Practice quiz

Which class builds an animation by calling an update function once per frame?

  • FuncAnimation
  • ArtistAnimation
  • AnimationWriter
  • FrameLoop

Answer: FuncAnimation. FuncAnimation from matplotlib.animation calls your update function once per frame.

Why must you assign the animation to a variable like anim = FuncAnimation(...)?

  • To set the frame rate
  • To enable blitting
  • So it isn't garbage-collected and silently stops
  • To save it as a GIF

Answer: So it isn't garbage-collected and silently stops. Without a kept reference, the animation object can be garbage-collected and freezes.

What does the interval argument to FuncAnimation control?

  • The number of frames
  • The delay between frames in milliseconds
  • The figure size
  • The line width

Answer: The delay between frames in milliseconds. interval is the delay between frames in milliseconds.

What is the purpose of blit=True?

  • It saves the animation
  • It loops the animation forever
  • It increases the frame count
  • It redraws only the parts that changed for smoother playback

Answer: It redraws only the parts that changed for smoother playback. blit=True redraws only changed artists, making playback smoother.

When blit=True, what must your update function do?

  • Return the iterable of artists it modified
  • Call plt.show() itself
  • Return None
  • Delete the figure

Answer: Return the iterable of artists it modified. With blitting, update must return the artists it changed, e.g. return (line,).

Which method updates a line's x and y data each frame?

  • line.refresh()
  • line.set_data(x, y)
  • line.redraw()
  • line.update_xy()

Answer: line.set_data(x, y). set_data(x, y) swaps the data on an existing line artist.

Which writer saves an animation as a GIF without extra installs?

  • ffmpeg
  • imagemagick
  • pillow
  • html5

Answer: pillow. anim.save('out.gif', writer='pillow') uses the bundled Pillow writer.

What is the role of init_func in FuncAnimation?

  • It sets the frame count
  • It chooses the colormap
  • It exports the file
  • It paints the clean starting state, useful with blitting

Answer: It paints the clean starting state, useful with blitting. init_func draws the initial blank state so blitting has a clean background.

What does the comma do in line, = ax.plot([], [])?

  • It unpacks the single line out of the returned list
  • It creates two lines
  • It enables animation
  • It is a syntax error

Answer: It unpacks the single line out of the returned list. plot returns a list; the comma unpacks the one line so you can call set_data on it.

Why does set_data expect [cos] and [sin] rather than bare floats?

  • To rotate the marker
  • Because set_data expects sequences, not scalars
  • To change the color
  • To speed up the animation

Answer: Because set_data expects sequences, not scalars. set_data needs sequences; recent Matplotlib raises an error on bare scalars.