👀
Crash Visualization
  • Welcome
  • Preface
    • Who the book is written for
    • How the book is organized
  • 1. Introduction of Data Visualization
    • 1.1 What is data visualization?
    • 1.2 Why does visualization matter?
  • 2. Tricks in Visualization
    • 2.1 Choose Appropriate Chart
    • 2.2 Features of Charts
      • 2.2.1 Table
      • 2.2.2 Column Chart
      • 2.2.3 Line Chart
      • 2.2.4 Pie Chart
      • 2.2.5 Scatter Chart
      • 2.2.6 Map Chart
    • 2.3 Misused Graph
    • 2.4 Tips in Visualization
  • 3. Matplotlib
    • 3.1 Basic Concepts
    • 3.2 Line Chart
    • 3.3 Area Chart
    • 3.4 Column Chart
    • 3.5 Histogram Chart
    • 3.6 Scatter Chart
    • 3.7 Lollipop Chart
    • 3.8 Pie Chart
    • 3.9 Venn Chart
    • 3.10 Waffle Chart
    • 3.11 Animation
  • 4. Seaborn
    • 4.1 Trends
    • 4.2 Ranking
      • 4.2.1 Barplot
      • 4.2.2 Boxplot
    • 4.3 Composition
      • 4.3.1 Stacked Chart
    • 4.4 Correlation
      • 4.4.1 Scatter Plot
      • 4.4.2 Linear Relationship
      • 4.4.3 Heatmap
      • 4.4.4 Pairplot
    • 4.5 Distribution
      • 4.5.1 Boxplot
      • 4.5.2 Violin plot
      • 4.5.3 Histogram plot
      • 4.5.4 Density plot
      • 4.5.5 Joint plot
  • 5. Bokeh
    • 5.1 Basic Plotting
    • 5.2 Data Sources
    • 5.3 Annotations
    • 5.4 Categorical Data
    • 5.5 Presentation and Layouts
    • 5.6 Linking and Interactions
    • 5.7 Network Graph
    • 5.8 Widgets
  • 6. Plotly
    • 6.1 Fundamental Concepts
      • 6.1.1 Plotly Express
      • 6.1.2 Plotly Graph Objects
    • 6.2 Advanced Charts
      • 6.2.1 Advanced Scatter Chart
      • 6.2.2 Advanced Bar Chart
      • 6.2.3 Advanced Pie Chart
      • 6.2.4 Advanced Heatmap
      • 6.2.5 Sankey Chart
      • 6.2.6 Tables
    • 6.3 Statistical Charts
      • 6.3.1 Common Statistical Charts
      • 6.3.2 Dendrograms
      • 6.3.3 Radar Chart
      • 6.3.4 Polar Chart
      • 6.3.5 Streamline Chart
    • 6.4 Financial Charts
      • 6.4.1 Funnel Chart
      • 6.4.2 Candlestick Chart
      • 6.4.3 Waterfall Chart
  • Support
    • Donation
Powered by GitBook
On this page
  • 1. Why Animation?
  • 2. Animations in matplotlib
  • 3. Basic Animation
  • 4. Subplots Animation
  • 5. 3D Animation
  • 6. Fun Examples

Was this helpful?

  1. 3. Matplotlib

3.11 Animation

Previous3.10 Waffle ChartNext4. Seaborn

Last updated 4 years ago

Was this helpful?

1. Why Animation?

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:

3. Basic Animation

An animation in six steps:

  1. import the necessary modules

  2. set up the plotting area

  3. create some data to plot

  4. plot the first line

  5. create a function to update the line

  6. call FuncAnimation and show

# step 1
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# step 2
fig = plt.figure()
ax = plt.axes(xlim=(0, 5), ylim=(-2, 2))


# step 3
line, = ax.plot([], [], lw=3,color = '#f93753')


# step 4
def init():
    line.set_data([], [])
    return line
    
# step 5    
def animate(i):
    x = np.linspace(0, 4, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

# step 6
anim = FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=10, blit=True)
plt.show()

4. Subplots Animation

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()

: makes an animation by repeatedly calling a function func. It is the most convenient one to use.

: Animation using a fixed set of Artist objects.

We can use 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.

FuncAnimation
ArtistAnimation
Celluloid
Raindrop Animation
3D Tulip Animation