A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/graph-plotting-python-set-3/ below:

Graph Plotting in Python | Set 3

Graph Plotting in Python | Set 3

Last Updated : 09 Feb, 2018

Graph Plotting in Python | Set 1 Graph Plotting in Python | Set 2

Matplotlib 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: Python
# importing required modules
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

# create a figure, axis and plot element
fig = plt.figure()
ax = plt.axes(xlim=(-50, 50), ylim=(-50, 50))
line, = ax.plot([], [], lw=2)

# initialization function
def init():
    # creating an empty plot/frame
    line.set_data([], [])
    return line,

# lists to store x and y axis points
xdata, ydata = [], []

# animation function
def animate(i):
    # t is a parameter
    t = 0.1*i
    
    # x, y values to be plotted
    x = t*np.sin(t)
    y = t*np.cos(t)
    
    # appending new points to x, y axes points list
    xdata.append(x)
    ydata.append(y)
    
    # set/update the x and y axes data
    line.set_data(xdata, ydata)
    
    # return line object
    return line,
    
# setting a title for the plot
plt.title('A growing coil!')
# hiding the axis details
plt.axis('off')

# call the animator    
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=500, interval=20, blit=True)

# save the animation as mp4 video file
anim.save('animated_coil.mp4', writer = 'ffmpeg', fps = 30)

# show the plot
plt.show()

Here is how the output animation looks like:

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

Example 2

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

Python
# importing required modules
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

# create a figure, axis and plot element
fig = plt.figure()
ax = plt.axes(xlim=(-25, 25), ylim=(-25, 25))
line, = ax.plot([], [], lw=2)

# initialization function
def init():
    # creating an empty plot/frame
    line.set_data([], [])
    return line,

# set of points for a star (could be any curve)
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)

# animation function
def animate(i):
    # t is a parameter
    t = 0.1*i
    
    # x, y values to be plotted
    X = x*np.cos(t) - y*np.sin(t)
    Y = y*np.cos(t) + x*np.sin(t)
    
    # set/update the x and y axes data
    line.set_data(X, Y)
    
    # return line object
    return line,
    
# setting a title for the plot
plt.title('A rotating star!')
# hiding the axis details
plt.axis('off')

# call the animator    
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=100, interval=100, blit=True)

# save the animation as mp4 video file
anim.save('basic_animation.mp4', writer = 'ffmpeg', fps = 10)

# show the plot
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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4