Để gán màu cụ thể cho từng ô trong bảng Matplotlib:
- Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
- Tạo tuple cho thuộc tính columns.
- Tạo list of lists dữ liệu bản ghi.
- Tạo list of lists màu từng ô.
- Tạo figure và subplots.
- Thêm table vào axes ax.
- Tắt axes.
- Hiển thị hình bằng show().
Ví dụ
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
columns = ('name', 'age', 'marks', 'salary')
cell_text = [["John", "23", "98", "234"], ["James", "24", "90", "239"]]
colors = [["red", "yellow", "blue", "green"], ["blue", "green", "yellow", "red"]]
fig, ax = plt.subplots()
the_table = ax.table(cellText=cell_text, cellColours=colors,
colLabels=columns, loc='center')
ax.axis('off')
plt.show()
Kết quả
