Sử dụng phương thức FuncAnimation, chúng ta có thể tạo một phim. Chúng ta sẽ tạo phương thức do người dùng định nghĩa, update, để liên tục thay đổi vị trí các hạt và cuối cùng phương thức trả về instance scatter.
Các bước
Lấy vị trí ban đầu, vận tốc, lực và kích thước của các hạt.
Tạo figure mới hoặc kích hoạt figure hiện có với figsize = (7, 7).
Thêm axes vào figure hiện tại và làm cho nó thành axes hiện tại, với xlim và ylim.
Vẽ scatter cho vị trí ban đầu của các hạt.
Tạo animation bằng cách gọi lặp lại hàm *func*. Chúng ta có thể truyền phương thức do người dùng định nghĩa giúp thay đổi vị trí hạt vào lớp FuncAnimation.
Hiển thị figure bằng plt.show().
Ví dụ
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
dt = 0.005
n = 20
L = 1
particles = np.zeros(n, dtype=[("position", float, 2),
("velocity", float, 2),
("force", float, 2),
("size", float, 1)])
particles["position"] = np.random.uniform(0, L, (n, 2))
particles["velocity"] = np.zeros((n, 2))
particles["size"] = 0.5 * np.ones(n)
fig = plt.figure(figsize=(7, 7))
ax = plt.axes(xlim=(0, L), ylim=(0, L))
scatter = ax.scatter(particles["position"][:, 0], particles["position"][:, 1])
def update(frame_number):
particles["force"] = np.random.uniform(-2, 2., (n, 2))
particles["velocity"] = particles["velocity"] + particles["force"] * dt
particles["position"] = particles["position"] + particles["velocity"] * dt
particles["position"] = particles["position"] % L
scatter.set_offsets(particles["position"])
return scatter,
anim = FuncAnimation(fig, update, interval=10)
plt.show()
Kết quả
