一、Matplotlib Animation簡介
Matplotlib是Python中常用的繪圖庫。其官方提供的animation模塊是利用FuncAnimation實現動畫效果的核心模塊。animation模塊中包含Animation類和FuncAnimation類,後者是Animation的一個子類。FuncAnimation提供了一種基於幀的動畫的方法,即通過更新數據並繪製圖像來實現動態圖的效果。
在實際應用過程中,動態圖能比靜態圖更直觀地展示數據的變化,如在時間序列分析或仿真過程中,可以利用動態圖很好地展示數據變化的過程。同時,動態圖也根據用戶的需求可以自定義視圖和交互方式。
以下是簡單示例代碼:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
def update_line(num, data, line):
line.set_data(data[..., :num])
return line,
fig, ax = plt.subplots()
data = np.random.rand(2, 25)
line, = ax.plot(data[0, 0:1], data[1, 0:1])
ani = animation.FuncAnimation(fig, update_line, frames=25, fargs=(data, line),
interval=50, blit=True)
plt.show()
二、Matplotlib動畫模塊常用可視化效果
1.基本圖形渲染
Matplotlib動畫可以實現基本圖形渲染,如折線圖、散點圖等。基本圖形渲染的數據更新通過update函數實現,每次update函數被調用時,圖形的渲染結果被重新繪製並顯示在界面中。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as ani
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
fig, ax = plt.subplots()
line, = ax.plot(x, y)
def update(num):
line.set_ydata(np.sin(x+num))
return line,
ani = ani.FuncAnimation(fig, update, frames=10, interval=200)
plt.show()
2.多個圖形渲染
動畫模塊還可以實現多個圖形的渲染,例如同時繪製折線圖和直方圖。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as ani
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
x = np.linspace(0, 10, num=100)
y = np.sin(x)
line, = ax1.plot([], [], lw=2)
ax1.set_xlim(0, 10), ax1.set_ylim(-1, 1)
rects = ax2.bar([], [])
def init():
line.set_data([], [])
rects.set_height(10)
return line, rects
def animate(i):
line.set_data(x[:i], y[:i])
data = np.random.rand(10)
rects.set_height(data)
rects.set_color('r')
return line, rects
anim = ani.FuncAnimation(fig, animate, init_func=init, frames=100,
interval=100, blit=True)
plt.show()
三、Matplotlib動畫模塊常見案例
1.簡單的心電圖動態效果
在健康檢查或醫學研究中,心電圖是一種常用的監控方式。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
fig = plt.figure()
data = np.random.normal(0, 1, (1000, 1)).cumsum(0)
def update(i):
plt.cla()
plt.plot(data[:i])
plt.ylim(-50, 50)
plt.yticks(np.arange(-50, 50, 10))
plt.grid(True)
ani = animation.FuncAnimation(fig, update, frames=len(data), interval=50)
plt.show()
2.簡單粒子演示效果
演示效果是指在動畫中繪製一系列的對象,其中每個對象都是在動畫中相對於另一個對象變化的。一種公共的演示效果是沙粒演示,其中顆粒在動畫中隨機地移動和相互運動,這些效果模擬了許多真實世界物理系統的動態。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
n_particles = 50
n_steps = 50
rx = np.random.uniform(-1, 1, n_particles)
ry = np.random.uniform(-1, 1, n_particles)
velx = np.random.normal(0, 0.1, n_particles)
vely = np.random.normal(0, 0.1, n_particles)
fig, ax = plt.subplots()
scat, = ax.plot([], [], ".")
def update(t, rx, ry, velx, vely):
rx = (rx + velx) % 1.0
ry = (ry + vely) % 1.0
scat.set_data(rx, ry)
return scat,
ani = animation.FuncAnimation(fig, update, frames=n_steps, fargs=(rx, ry, velx, vely),
interval=50, blit=True)
plt.show()
3.簡單的飛行器動畫
該動畫基於二維流體力學的概念,演示了一個以勻速飛行的飛機離開起飛場。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
M = 100
x = np.cumsum(np.random.randn(M))
y = np.cumsum(np.random.randn(M))
im = ax.imshow(np.zeros((100, 100)), vmin=-1, vmax=1)
def update(j):
im.set_data(np.random.randn(100, 100))
im.set_extent([x[j]-50, x[j]+50, y[j]-50, y[j]+50])
return im
ani = animation.FuncAnimation(fig, update, frames=M, interval=50)
plt.show()
原創文章,作者:FODUF,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/363911.html
微信掃一掃
支付寶掃一掃