Để cập nhật biểu đồ cột động trong Matplotlib, 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 figure mới hoặc kích hoạt figure hiện có.
- Tạo danh sách điểm dữ liệu và màu sắc.
- Vẽ các cột với data và colors, dùng phương thức bar().
- Dùng lớp FuncAnimation(), tạo animation bằng cách gọi liên tục hàm animation đặt height của cột và facecolor của các cột.
- Để hiển thị figure, dùng phương thức show().
Ví dụ
import numpy as np
from matplotlib import animation as animation, pyplot as plt, cm
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
data = [1, 4, 3, 2, 6, 7, 3]
colors = ['red', 'yellow', 'blue', 'green', 'black']
bars = plt.bar(data, data, facecolor='green', alpha=0.75)
def animate(frame):
global bars
index = np.random.randint(1, 7)
bars[frame].set_height(index)
bars[frame].set_facecolor(colors[np.random.randint(0, len(colors))])
ani = animation.FuncAnimation(fig, animate, frames=len(data))
plt.show()
Kết quả
