Seaborn 子情节大量空白

数据挖掘 可视化 matplotlib 海运
2021-09-28 14:18:09

我正在尝试使用 Seaborns 子图方法在垂直列中绘制三个热图。

import seaborn as sns
initalCorr = inputX.corr()
secondaryCorr = inputX_corr.corr()
finalCorr = inputX[selected_columns_pvalue].corr()

fig, axs = plt.subplots(3, figsize = (15,45))
fig.suptitle('Heatmaps of correlation between features selected for training',
fontsize = 20)

sns.heatmap(data = initalCorr, center = 0, square = True, ax = axs[0])
axs[0].set_title('Correlation between all features', fontsize = 20)   

sns.heatmap(secondaryCorr, center = 0, square = True, annot = True, linewidths = .5, fmt = '.2f', ax = axs[1])
axs[1].set_title('Correlation between features slected for P-Value analysis', fontsize = 20)

sns.heatmap(finalCorr, center = 0, square = True, annot = True, linewidths = .5, fmt = '.2f', ax = axs[2])
axs[2].set_title('Correlation between finally selected features', fontsize = 20) 

图表本身绘制得很好,但标题相对于图表显得格格不入。

显示不合适的标题文本的图像

如何将标题直接放在热图上方?

附加:seaborn 0.9.0 版 matplotlib 3.0.3 版

1个回答

您可以将标题移近第一个图形。只需在代码末尾添加以下两行

fig.tight_layout()
fig.subplots_adjust(top=0.95)

减少空白