Graph Plotting in Python | Set 3 (original) (raw)

Last Updated : 09 Feb, 2018

Graph Plotting in Python | Set 1 Graph Plotting in Python | Set 2Matplotlib is a pretty extensive library which supports Animations of graphs as well. The animation tools center around the matplotlib.animation base class, which provides a framework around which the animation functionality is built. The main interfaces are TimedAnimation and FuncAnimation and out of the two, FuncAnimation is the most convenient one to use.Installation:

Implementation:

import matplotlib.pyplot as plt

import matplotlib.animation as animation

import numpy as np

fig = plt.figure()

ax = plt.axes(xlim = ( - 50 , 50 ), ylim = ( - 50 , 50 ))

line, = ax.plot([], [], lw = 2 )

def init():

`` line.set_data([], [])

`` return line,

xdata, ydata = [], []

def animate(i):

`` t = 0.1 * i

`` x = t * np.sin(t)

`` y = t * np.cos(t)

`` xdata.append(x)

`` ydata.append(y)

`` line.set_data(xdata, ydata)

`` return line,

plt.title( 'A growing coil!' )

plt.axis( 'off' )

anim = animation.FuncAnimation(fig, animate, init_func = init,

`` frames = 500 , interval = 20 , blit = True )

anim.save( 'animated_coil.mp4' , writer = 'ffmpeg' , fps = 30 )

plt.show()

Here is how the output animation looks like:

Now, let us try to understand the code in pieces:

This is the most important function of above program. animate() function is called again and again by the animator to create each frame. The number of times this function will be called is determined by number of frames, which is passed as frames argument to animator.animate() function takes the index of ith frame as argument.
t = 0.1i
Here, we cleverly use the index of current frame as a parameter!
x = t
np.sin(t)
y = t*np.cos(t)
Now, since we have the parameter t, we can easily plot any parametric equation. For example, here, we are plotting a spiral using its parametric equation.
line.set_data(xdata, ydata)
return line,
Finally, we use set_data() function to set x and y data and then return plot object, line .

Now, we create the FuncAnimation object, anim . It takes various arguments explained below:fig : figure to be plotted.animate : the function to be called repeatedly for each frame**.** init_func : function used to draw a clear frame. It is called once before the first frame.frames : number of frames. (Note: frames can also be an iterable or generator.)interval : duration between frames ( in milliseconds)blit : setting blit=True means that only those parts will be drawn, which have changed.

Example 2This example shows how one can make a rotating curve by applying some simple mathematics!

import matplotlib.pyplot as plt

import matplotlib.animation as animation

import numpy as np

fig = plt.figure()

ax = plt.axes(xlim = ( - 25 , 25 ), ylim = ( - 25 , 25 ))

line, = ax.plot([], [], lw = 2 )

def init():

`` line.set_data([], [])

`` return line,

p = np.arange( 0 , 4 * np.pi, 0.1 )

x = 12 * np.cos(p) + 8 * np.cos( 1.5 * p)

y = 12 * np.sin(p) - 8 * np.sin( 1.5 * p)

def animate(i):

`` t = 0.1 * i

`` X = x * np.cos(t) - y * np.sin(t)

`` Y = y * np.cos(t) + x * np.sin(t)

`` line.set_data(X, Y)

`` return line,

plt.title( 'A rotating star!' )

plt.axis( 'off' )

anim = animation.FuncAnimation(fig, animate, init_func = init,

`` frames = 100 , interval = 100 , blit = True )

anim.save( 'basic_animation.mp4' , writer = 'ffmpeg' , fps = 10 )

plt.show()

Here is how the output of above program looks like:

Here, we have used some simple mathematics to rotate a given curve.

All in all, animations are a great tool to create amazing stuff and many more things can be created using them. So, this was how animated plots can be generated and saved using Matplotlib.