Animation with matplotlib

Rotating animation with matplotlib

Introduction

matplotlib can not only create graphs, but also animations. The main animation functions are Artist Animation and Func Animation, but here, Func Animation is used to display a rotating animation as shown below.

Commentary

Module import

import.py


import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
import matplotlib.animation as animation
from IPython.display import HTML

Creating diagrams and displaying circles

makefig.py



fig, ax = plt.subplots(figsize=(6,6))

#plot circle
c1 = Circle((0, 0), 5,color='0.75')
ax.add_patch(c1)
c2 = Circle((0, 0), 4.5,color='0.7')
ax.add_patch(c2)
c3 = Circle((0, 0), 4,color='0.65')
ax.add_patch(c3)
c4 = Circle((0, 0), 3.5,color='0.6')
ax.add_patch(c4)
c5 = Circle((0, 0), 3,color='0.55')
ax.add_patch(c5)
c6 = Circle((0, 0), 2.5,color='0.5')
ax.add_patch(c6)
c7 = Circle((0, 0), 2,color='0.45')
ax.add_patch(c7)
c8 = Circle((0, 0), 1.5,color='0.4')
ax.add_patch(c8)
c9 = Circle((0, 0), 1,color='0.35')
ax.add_patch(c9)

Create a fig with plt.subplots and display 9 circles with Circle.

Parameters for moving the circle

param.py


def f(a,t):
    return np.cos(a*t)

def g(b,t,sig):
    return np.sin(b*t+sig)

t = np.linspace(0,2*np.pi,200)
sig = np.pi/4

x=f(3,t)
y=g(2,t,sig)
x1 = np.hstack([x,np.ones(8)])
y1 = np.hstack([y,np.ones(8)*np.sin(sig)])
x2 = np.hstack([np.ones(1),x,np.ones(7)])
y2 = np.hstack([np.ones(1)*np.sin(sig),y,np.ones(7)*np.sin(sig)])
x3 = np.hstack([np.ones(2),x,np.ones(6)])
y3 = np.hstack([np.ones(2)*np.sin(sig),y,np.ones(6)*np.sin(sig)])
x4 = np.hstack([np.ones(3),x,np.ones(5)])
y4 = np.hstack([np.ones(3)*np.sin(sig),y,np.ones(5)*np.sin(sig)])
x5 = np.hstack([np.ones(4),x,np.ones(4)])
y5 = np.hstack([np.ones(4)*np.sin(sig),y,np.ones(4)*np.sin(sig)])
x6 = np.hstack([np.ones(5),x,np.ones(3)])
y6 = np.hstack([np.ones(5)*np.sin(sig),y,np.ones(3)*np.sin(sig)])
x7 = np.hstack([np.ones(6),x,np.ones(2)])
y7 = np.hstack([np.ones(6)*np.sin(sig),y,np.ones(2)*np.sin(sig)])
x8 = np.hstack([np.ones(7),x,np.ones(1)])
y8 = np.hstack([np.ones(7)*np.sin(sig),y,np.ones(1)*np.sin(sig)])
x9 = np.hstack([np.ones(8),x])
y9 = np.hstack([np.ones(8)*np.sin(sig),y])

The circle moves along the Lissajous curve. Use np.hstack etc. to shift the movement of each circle little by little.

Display range setting

xlimylim.py


xmin, xmax = xlim = x.min()-5, x.max()+5
ymin, ymax = ylim = y.min()-5, y.max()+5
ax.set(xlim=xlim, ylim=ylim, autoscale_on=False)
ax.set_aspect('equal')
ax.axis('off')

Animation settings, saves, markings

xlimylim.py


def init():   
    return c1, c2, c3, c4, c5, c6, c7,c8,c9,

def update(num):
    c1.set_center([x1[num],y1[num]])
    c2.set_center([x2[num],y2[num]])
    c3.set_center([x3[num],y3[num]])
    c4.set_center([x4[num],y4[num]])
    c5.set_center([x5[num],y5[num]])
    c6.set_center([x6[num],y6[num]])
    c7.set_center([x7[num],y7[num]])
    c8.set_center([x8[num],y8[num]])
    c9.set_center([x9[num],y9[num]])
    
    return c1, c2, c3, c4, c5, c6, c7,c8,c9

ani = animation.FuncAnimation(fig, update, init_func=init,interval = 20, frames = 208)
ani.save('Tunnel_animation.mp4', writer="ffmpeg",dpi=100)
HTML(ani.to_html5_video())

init is the first process to be executed when the animation is executed, and since we do nothing here, we just return the circle. Since update is a function that is executed for animation, we move each circle with set_center.

In animation.FuncAnimation (), you can specify the switching interval of the figure with interval. The unit is ms. In frames, you can set the number of times the update function is executed.

You can save the animation at the specified resolution with ani.save ('filename.mp4', writer = "ffmpeg", dpi = 100). If ffmpeg is not included

ffmpeg.py


conda install -c conda-forge ffmpeg

Let's put it in. You can display the animation in jupyterlab or notebook with HTML (ani.to_html5_video ()).

reference

https://discourse.matplotlib.org/t/tunnel-animation/20733

Promotion

I have written articles about various matplotlib animations on Blog. Please see if you are interested.

https://sabopy.com/category/py/matplotlib-animation/

Recommended Posts

Animation with matplotlib
Animation with matplotlib
Create plot animation with Python + Matplotlib
Japanese with matplotlib
Histogram with matplotlib
Animate with matplotlib
Easy animation with matplotlib (mp4, gif)
2-axis plot with Matplotlib
Heatmap with Python + matplotlib
Band graph with matplotlib
Learn with Cheminformatics Matplotlib
Real-time drawing with matplotlib
Various colorbars with Matplotlib
3D plot with matplotlib
Adjust axes with matplotlib
The basis of graph theory with matplotlib animation
Graph Excel data with matplotlib (1)
Try using matplotlib with PyCharm
Diffusion equation animation with NumPy
Draw an Earth-like flow animation with matplotlib and cartopy
Graph drawing method with matplotlib
Graph Excel data with matplotlib (2)
Stackable bar plot with matplotlib
Gradient color selection with matplotlib
Bubble sort with fluffy animation
Animate multiple graphs with matplotlib
Make a gif animation from a serial number file with matplotlib
Visualize cavity flow with matplotlib and save as gif animation
A python graphing manual with Matplotlib.
Inference & result display with Tensorflow + matplotlib
Japaneseize Matplotlib with Alpine using Docker
[Python] font family and font with matplotlib
[IOS] Disassemble GIF animation with Pythonista3.
Draw Japanese with matplotlib on Ubuntu
Draw a loose graph with matplotlib
Versatile data plotting with pandas + matplotlib
Heatmap with Dendrogram in Python + matplotlib
Easy to draw graphs with matplotlib
Continuously color with matplotlib scatter plot
Draw Lyapunov Fractal with Python, matplotlib
When matplotlib doesn't work with python2.7
Lognormal probability plot with Python, matplotlib
Write a stacked histogram with matplotlib
Implement "Data Visualization Design # 2" with matplotlib
[Introduction to Matplotlib] Axes 3D animation: I played with 3D Lissajous figures ♬
How to title multiple figures with matplotlib
[Python] Set the graph range with matplotlib
Adjust the spacing between figures with Matplotlib
Flexible animation creation using animation.FuncAnimation of matplotlib
Matplotlib gallery
Try drawing a normal distribution with matplotlib
Matplotlib memo
Make a partially zoomed figure with matplotlib
Write SVG graphs with matplotlib on heroku
Display Japanese graphs with VS Code + matplotlib
Set the xticklabels color individually with matplotlib
[Python] Let's make matplotlib compatible with Japanese
Graph trigonometric functions with numpy and matplotlib
matplotlib summary
Display markers above the border with matplotlib
Make a GIF animation with folder monitoring