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.
— sabopy.com (@Sabopy_com) January 31, 2020
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
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.
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.
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')
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 ()).
https://discourse.matplotlib.org/t/tunnel-animation/20733
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