Exploring datasets is a big part of what many scientists do these days. In many cases, these datasets will have more than two dimensions. For example, temperature or salinity in an ocean circulation model has four dimensions: x, y, z, t.It’s futile to try and display these in a single plot. That’s where animation can help.
2. Animations in matplotlib
Matplotlib’s animation deals with the animation part. It provides a framework around which the animation functionality is built. There are two main interfaces to achieve that using:
FuncAnimation: makes an animation by repeatedly calling a function func. It is the most convenient one to use.
ArtistAnimation: Animation using a fixed set of Artist objects.
We can use Celluloid to simplify the process of creating animations in matplotlib. It creates a figure and creates a camera. Then it reuses figure and after each frame is created, take a snapshot with the camera. Finally, an animation is created with all the captured frames.
pip install Celluloid
import numpy as np
from matplotlib import pyplot as plt
from celluloid import Camera
fig, axes = plt.subplots(2)
camera = Camera(fig)
t = np.linspace(0, 3 * np.pi, 128, endpoint=False)
for i in t:
axes[0].plot(t, np.sin(t + i), color='#5ac9ff',linewidth = 3)
axes[1].plot(t, np.sin(t - i), color='#f96080',linewidth = 3, linestyle='dashed')
camera.snap()
animation = camera.animate()
plt.show()
5. 3D Animation
Let's draw a 3D tulip this time.
from matplotlib import animation,cm
from mpl_toolkits.mplot3d import Axes3D
# create a figure
fig = plt.figure(figsize = (6,6))
# initialise 3D Axes
ax = Axes3D(fig)
# remove background grid, fill and axis
ax.grid(False)
ax.xaxis.pane.fill = ax.yaxis.pane.fill = ax.zaxis.pane.fill = False
plt.axis('off')
# Make data
X = np.arange(-5, 5, 0.1)
Y = np.arange(-5, 5, 0.1)
xx, yy = np.meshgrid(X, Y)
r = np.sqrt(xx**2 + yy**2)
z = np.cos(r)
# create the initialiser with the surface plot
def init():
ax.plot_surface(xx, yy, z, cmap=cm.RdPu,
linewidth=0, antialiased=False)
return fig,
# create animate function, this will adjust the view one step at a time
def animate(i):
ax.view_init(elev=30.0, azim=i)
return fig,
# create the animated plot
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=360, interval=20, blit=True)
6. Fun Examples
fig = plt.figure()
ax = plt.axes(xlim=(-50, 50), ylim=(-50, 50))
line, = ax.plot([], [], lw=2,color = '#f93753')
# initialization function
def init():
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)
line.set_data(xdata, ydata)
return line,
# setting a title for the plot
plt.title('Draw a big lollipop')
# hiding the axis details
plt.axis('off')
# call the animator
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=500, interval=10, blit=True)
plt.show()