Để cập nhật giá trị trục X bằng Matplotlib animation, chúng ta có thể thực hiện các bước sau:
- Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
- Tạo một figure và một tập subplots.
- Tạo điểm dữ liệu x và y bằng numpy.
- Vẽ x và y bằng phương thức plot trên trục (ax).
- Tạo animation bằng cách gọi liên tục hàm animate thiết lập giá trị trục X theo frame.
- Để hiển thị figure, dùng phương thức show().
Ví dụ
import matplotlib.pylab as plt
import matplotlib.animation as animation
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
x = np.linspace(0, 15, 100)
y = np.sin(x)
ax.plot(x, y, lw=7)
def animate(frame):
ax.set_xlim(left=0, right=frame)
ani = animation.FuncAnimation(fig, animate, frames=10)
plt.show()
Kết quả

