Để vẽ ma trận ảnh động trong Matplotlib:
Các bước thực hiện
- Thiết lập kích thước hình và padding
- Tạo hình và tập subplot
- Tạo animation bằng cách gọi lặp lại hàm update
- Trong
update: tạo ma trận 6×6 ngẫu nhiên, hiển thị bằngimshow() - Tắt trục bằng
set_axis_off() - Hiển thị bằng
show()
Ví dụ minh họa
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
def update(i):
im_normed = np.random.rand(6, 6)
ax.imshow(im_normed)
ax.set_axis_off()
anim = FuncAnimation(fig, update, frames=20, interval=50)
plt.show()
Đầu ra
Animation GIF hiển thị ma trận 6×6 ngẫu nhiên thay đổi liên tục.
