เพื่อสร้างแอนิเมชันของ pcolormesh ใน Matplotlib สามารถใช้คลาส Animation ได้
ขั้นตอน
- สร้าง Figure และชุด subplots
- สร้างข้อมูล x, y และ t ด้วย NumPy
- สร้างเมทริกซ์พิกัด X3, Y3, T3 ด้วย meshgrid
- คำนวณค่า G สำหรับ pseudocolor plot
- สร้าง pseudocolor plot ด้วย pcolormesh()
- เพิ่มแถบสี (colorbar) สำหรับ pcolormesh
- สร้างแอนิเมชันด้วยคลาส FuncAnimation
- แสดงกราฟด้วย show()
ตัวอย่างโค้ด
import numpy as np
from matplotlib import pyplot as plt, animation
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
x = np.linspace(-3, 3, 91)
t = np.linspace(0, 25, 30)
y = np.linspace(-3, 3, 91)
X3, Y3, T3 = np.meshgrid(x, y, t)
sinT3 = np.sin(2 * np.pi * T3 / T3.max(axis=2)[..., np.newaxis])
G = (X3 ** 2 + Y3 ** 2) * sinT3
cax = ax.pcolormesh(x, y, G[:-1, :-1, 0], vmin=-1, vmax=1, cmap='Blues')
fig.colorbar(cax)
def animate(i):
cax.set_array(G[:-1, :-1, i].flatten())
anim = animation.FuncAnimation(fig, animate, interval=100, frames=len(t) - 1)
anim.save('517.gif')
plt.show()
ผลลัพธ์
