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

Ghi chú (annotate) từng ô heatmap trong Seaborn

Để hiển thị giá trị số liệu lên từng ô của heatmap, đặt annot=True trong sns.heatmap(). Tùy chỉnh font size bằng annot_kws.

Các bước

  • Thiết lập figure size và autolayout.
  • Tạo DataFrame 5x5 ngẫu nhiên.
  • Vẽ heatmap: sns.heatmap(df, annot=True, annot_kws={"size": 7}).
  • Hiển thị bằng show().

Ví dụ

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"])
sns.heatmap(df, annot=True, annot_kws={"size": 7})
plt.show()

Kết quả

Ghi chú (annotate) từng ô heatmap trong Seaborn