Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tạo animation đường sin (sine curve) trong Matplotlib

Để tạo animation đường sin chạy (travelling wave), dùng FuncAnimation với hàm init() và animate(i) cập nhật dữ liệu đường.

Các bước

  • Thiết lập figure size và autolayout.
  • Tạo figure, axes với xlim, ylim cố định.
  • Vẽ đường rỗng ban đầu: line, = ax.plot([], [], lw=2).
  • Hàm init() trả về line rỗng.
  • Hàm animate(i) cập nhật y = sin(2π(x - 0.01*i)).
  • Tạo FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True).
  • Hiển thị bằng show().

Ví dụ

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
def init():
    line.set_data([], [])
    return line,
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)
plt.show()

Kết quả

Tạo animation đường sin (sine curve) trong Matplotlib