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

Cách vẽ ma trận ảnh động (animated image matrix) trong Matplotlib

Để vẽ ma trận ảnh động trong Matplotlib:

Các bước thực hiện

  1. Thiết lập kích thước hình và padding
  2. Tạo hình và tập subplot
  3. Tạo animation bằng cách gọi lặp lại hàm update
  4. Trong update: tạo ma trận 6×6 ngẫu nhiên, hiển thị bằng imshow()
  5. Tắt trục bằng set_axis_off()
  6. 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.

Cách vẽ ma trận ảnh động (animated image matrix) trong Matplotlib