Matplotlib.animation.FuncAnimation class in Python

07 Oct, 2021
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. 
The matplotlib.animation.FuncAnimation class is used to make animation by repeatedly calling the same function (ie, func). 
Syntax: class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, *, cache_frame_data=True, **kwargs)Paramwrite.geeksforgeeks.org/wp-admin/post.php?post=1675187&action=editors:  
fig: It is the figure object used for drawing, resizing or any other needed events. Any additional positional arguments can be supplied via the fargs parameter. func: It is the callable function that gets called each time. The next value in frames is given through the first argument. Any other additional positional arguments is given via the fargs parameter. If the blit value is equal to True then, func returns an iterable of all artists that are to be modified or created. This data is used by the blitting algorithm to decide which parts of the figure has to be updated. If blit== False then the value returned is unused or omitted. frames: It is either an iterable, an integer, a generator function or None. It is an optional argument. It is the data source to be passed to func and every frame of the animation. init_func: It is an optional callable function that is used to draw a clear frame. fargs: It is an optional parameter that is either a tuple or None, which is an additional arguments that needs to be passed to each call to func. save_count: It is an integer that acts as fallback for the number of values from frames to cache. This is only used if the number of frames cannot be inferred from frames, i.e. when it’s an iterator without length or a generator.Its default is 100. interval: It is an optional integer value that represents the delay between each frame in milliseconds. Its default is 100. repeat_delay: It is an optional integer value that adds a delay in milliseconds before repeating the animation. It defaults to None. blit: It is an optional boolean argument used to control blitting to optimize drawing. cache_frame_data: It is an optional boolean argument used to control cacheing of data. It defaults to True.  
fig: It is the figure object used for drawing, resizing or any other needed events. Any additional positional arguments can be supplied via the fargs parameter. 
func: It is the callable function that gets called each time. The next value in frames is given through the first argument. Any other additional positional arguments is given via the fargs parameter. If the blit value is equal to True then, func returns an iterable of all artists that are to be modified or created. This data is used by the blitting algorithm to decide which parts of the figure has to be updated. If blit== False then the value returned is unused or omitted. 
frames: It is either an iterable, an integer, a generator function or None. It is an optional argument. It is the data source to be passed to func and every frame of the animation. 
init_func: It is an optional callable function that is used to draw a clear frame. 
fargs: It is an optional parameter that is either a tuple or None, which is an additional arguments that needs to be passed to each call to func. 
save_count: It is an integer that acts as fallback for the number of values from frames to cache. This is only used if the number of frames cannot be inferred from frames, i.e. when it’s an iterator without length or a generator.Its default is 100. 
interval: It is an optional integer value that represents the delay between each frame in milliseconds. Its default is 100. 
repeat_delay: It is an optional integer value that adds a delay in milliseconds before repeating the animation. It defaults to None. 
blit: It is an optional boolean argument used to control blitting to optimize drawing. 
cache_frame_data: It is an optional boolean argument used to control cacheing of data. It defaults to True.  
 
Methods of the class:
 
Example 1:  
Python3
import matplotlib.animation as animationimport matplotlib.pyplot as pltimport numpy as np  # creating a blank window# for the animationfig = plt.figure()axis = plt.axes(xlim =(-50, 50),                ylim =(-50, 50)) line, = axis.plot([], [], lw = 2) # what will our line dataset# contain?def init():    line.set_data([], [])    return line, # initializing empty values# for x and y co-ordinatesxdata, ydata = [], [] # animation functiondef animate(i):    # t is a parameter which varies    # with the frame number    t = 0.1 * i         # x, y values to be plotted    x = t * np.sin(t)    y = t * np.cos(t)         # appending values to the previously    # empty x and y data holders    xdata.append(x)    ydata.append(y)    line.set_data(xdata, ydata)         return line, # calling the animation function    anim = animation.FuncAnimation(fig, animate,                            init_func = init,                            frames = 500,                            interval = 20,                            blit = True) # saves the animation in our desktopanim.save('growingCoil.mp4', writer = 'ffmpeg', fps = 30)
Output:  
Example 2:  
Python3
from matplotlib import pyplot as pltimport numpy as npfrom matplotlib.animation import FuncAnimation # initializing a figure in# which the graph will be plottedfig = plt.figure() # marking the x-axis and y-axisaxis = plt.axes(xlim =(0, 4),                ylim =(-2, 2)) # initializing a line variableline, = axis.plot([], [], lw = 3) # data which the line will# contain (x, y)def init():    line.set_data([], [])    return line, def animate(i):    x = np.linspace(0, 4, 1000)     # plots a sine graph    y = np.sin(2 * np.pi * (x - 0.01 * i))    line.set_data(x, y)         return line, anim = FuncAnimation(fig, animate,                    init_func = init,                    frames = 200,                    interval = 20,                    blit = True) anim.save('continuousSineWave.mp4',          writer = 'ffmpeg', fps = 30)
Output:  
 
gabaa406
Python-matplotlib
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
Convert integer to string in Python
Convert string to integer in Python
How to set input type date in dd-mm-yyyy format using HTML ?
Python infinity
Factory method design pattern in Java