Để tạo hoạt ảnh cho colorbar trong matplotlib, ta thực hiện:
- Thiết lập kích thước hình và điều chỉnh khoảng đệm giữa và xung quanh subplots.
- Tạo hình mới hoặc kích hoạt hình hiện có.
- Thêm '~.axes.Axes' vào hình như phần subplot.
- Khởi tạo Divider dựa trên trục hiện có (ax object) trả về axis locator mới cho ô chỉ định.
- Tạo trục tại *position* với cùng height (hoặc width) trục chính.
- Tạo dữ liệu ngẫu nhiên dùng numpy.
- Dùng imshow() vẽ dữ liệu ngẫu nhiên.
- Đặt tiêu đề đồ thị.
- Khởi tạo danh sách colormaps.
- Tạo hoạt ảnh colorbar dùng animate().
- Hiển thị hình dùng show().
Ví dụ
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
div = make_axes_locatable(ax)
cax = div.append_axes('right', '5%', '5%')
data = np.random.rand(5, 5)
im = ax.imshow(data)
cb = fig.colorbar(im, cax=cax)
tx = ax.set_title('Frame 0')
cmap = ["copper", 'RdBu_r', 'Oranges', 'cividis', 'hot', 'plasma']
def animate(i):
cax.cla()
data = np.random.rand(5, 5)
im = ax.imshow(data, cmap=cmap[i % len(cmap)])
fig.colorbar(im, cax=cax)
tx.set_text('Frame {0}'.format(i))
ani = animation.FuncAnimation(fig, animate, frames=10)
plt.show()
Kết quả
