Để kết hợp hai heatmap trong seaborn, thực hiện các bước sau:
Các bước
- Thiết lập kích thước hình và padding.
- Tạo hai Pandas DataFrame.
- Tạo figure và subplots ax1, ax2.
- Vẽ ma trận màu trên ax1 và ax2.
- Di chuyển ticks và ticklabels sang phải axes.
- Giảm padding giữa subplots al tối thiểu.
- Hiển thị hình bằng show().
Ví dụ
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df1 = pd.DataFrame(np.random.rand(10, 4), columns=list("ABCD"))
df2 = pd.DataFrame(np.random.rand(10, 4), columns=list("WXYZ"))
fig, (ax1, ax2) = plt.subplots(ncols=2)
fig.subplots_adjust(wspace=0.01)
sns.heatmap(df1, cmap="plasma", ax=ax1, cbar=False)
sns.heatmap(df2, cmap="hot", ax=ax2, cbar=False)
ax2.yaxis.tick_right()
fig.subplots_adjust(wspace=0.001)
plt.show()
Kết quả
Hai heatmap cạnh nhau, chia sẻ trục y, tick labels bên phải heatmap thứ hai, khoảng cách gần như không.