"""
.. redirect-from:: /tutorials/introductory/animation_tutorial
.. _animations:
===========================
Animations using Matplotlib
===========================
Based on its plotting functionality, Matplotlib also provides an interface to
generate animations using the `~matplotlib.animation` module. An
animation is a sequence of frames where each frame corresponds to a plot on a
`~matplotlib.figure.Figure`. This tutorial covers a general guideline on
how to create such animations and the different options available. More information is available in the API description: `~matplotlib.animation`
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
# %%
# Animation classes
# =================
#
# The animation process in Matplotlib can be thought of in 2 different ways:
#
# - `~matplotlib.animation.FuncAnimation`: Generate data for first
# frame and then modify this data for each frame to create an animated plot.
#
# - `~matplotlib.animation.ArtistAnimation`: Generate a list (iterable)
# of artists that will draw in each frame in the animation.
#
# `~matplotlib.animation.FuncAnimation` is more efficient in terms of
# speed and memory as it draws an artist once and then modifies it. On the
# other hand `~matplotlib.animation.ArtistAnimation` is flexible as it
# allows any iterable of artists to be animated in a sequence.
#
# ``FuncAnimation``
# -----------------
#
# The `~matplotlib.animation.FuncAnimation` class allows us to create an
# animation by passing a function that iteratively modifies the data of a plot.
# This is achieved by using the *setter* methods on various
# `~matplotlib.artist.Artist` (examples: `~matplotlib.lines.Line2D`,
# `~matplotlib.collections.PathCollection`, etc.). A usual
# `~matplotlib.animation.FuncAnimation` object takes a
# `~matplotlib.figure.Figure` that we want to animate and a function
# *func* that modifies the data plotted on the figure. It uses the *frames*
# parameter to determine the length of the animation. The *interval* parameter
# is used to determine time in milliseconds between drawing of two frames.
# Animating using `.FuncAnimation` typically requires these steps:
#
# 1) Plot the initial figure as you would in a static plot. Save all the created
# artists, which are returned by the plot functions, in variables so that you can
# access and modify them later in the animation function.
# 2) Create an animation function that updates the artists for a given frame.
# Typically, this calls ``set_*`` methods of the artists.
# 3) Create a `.FuncAnimation`, passing the `.Figure` and the animation function.
# 4) Save or show the animation using one of the following methods:
#
# - `.pyplot.show` to show the animation in a window
# - `.Animation.to_html5_video` to create a HTML ``