尝试使用子图在同一轴上打印两个热图

Trying to print two heatmaps on the same axis using subplots

我正在尝试使用具有相同 y 轴的子图并排打印两个热图。我正在使用 seaborn 绘制它们。

这是我的两个热图代码。数据是具有 45 列的大型数据框的一部分。我为下面的两个热图分别使用了 4 列。

sns.heatmap(genre_top10.iloc[:,[13,16,19,22]], annot = True, fmt = '.0f', linewidths=.5)
plt.show()
sns.heatmap(genre_top10.iloc[:,[14,17,20,23]], annot = True, fmt = '.0f', linewidths=.5)
plt.show()

但无法弄清楚如何将它们打印为具有共享 y 轴的子图。请帮忙。

您可以使用 plt.subplots 并将热图分配给不同的轴。我不确定你的数据结构,但像这样的东西会将热图添加到不同的轴上。

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

genre_top10 = pd.DataFrame(np.random.rand(4,30))

fig, ax = plt.subplots(ncols=1, nrows=2)
sns.heatmap(genre_top10.iloc[:,[13,16,19,22]], annot = True, fmt = '.0f', linewidths=.5, ax=ax[0])
sns.heatmap(genre_top10.iloc[:,[14,17,20,23]], annot = True, fmt = '.0f', linewidths=.5, ax=ax[1])
plt.show()