简体   繁体   English

Python:如何并排绘制多个seaborn热图?

[英]Python: How to plot multiple seaborn heatmaps side-by-side?

I have a Pandas dataframe with 20+ features. 我有一个具有20多个功能的Pandas数据框。 I would like to see their correlation matrices. 我想看看他们的相关矩阵。 I create the heatmaps with code like the below, with subset1 , subset2 , etc.: 我使用以下代码, subset1subset2等创建热图:

import seaborn as sns
cmap = sns.diverging_palette( 220 , 10 , as_cmap = True )
sb1 = sns.heatmap(
    subset1.corr(), 
    cmap = cmap,
    square=True, 
    cbar_kws={ 'shrink' : .9 }, 
    annot = True, 
    annot_kws = { 'fontsize' : 12 })

I would like to be able to display multiple heatmaps generated by the above code, side-by-side like so: 我希望能够并排显示上述代码生成的多个热图:

display_side_by_side(sb1, sb2, sb3, . . .)

I'm not sure how to do this because the first code chunk above not only saves the results to sb1 , but also plots the heatmap. 我不确定如何执行此操作,因为上面的第一个代码块不仅将结果保存到sb1 ,而且还会绘制热图。 Also, not sure how to write a function, display_side_by_side() . 另外,不确定如何编写函数display_side_by_side() I am using the following for Pandas dataframes: 我对Pandas数据框使用以下内容:

# create a helper function that takes pd.dataframes as input and outputs pretty, compact EDA results
from IPython.display import display_html
def display_side_by_side(*args):
    html_str = ''
    for df in args:
        html_str = html_str + df.to_html()
    display_html(html_str.replace('table','table style="display:inline"'),raw=True)

Based on the first answer below by Simas Joneliunas, I have come up with the following working solution: 根据Simas Joneliunas在下面的第一个答案,我提出了以下可行的解决方案:

import matplotlib.pyplot as plt
import seaborn as sns

# Here we create a figure instance, and two subplots
fig = plt.figure(figsize = (20,20)) # width x height
ax1 = fig.add_subplot(3, 3, 1) # row, column, position
ax2 = fig.add_subplot(3, 3, 2)
ax3 = fig.add_subplot(3, 3, 3)
ax4 = fig.add_subplot(3, 3, 4)
ax5 = fig.add_subplot(3, 3, 5)

# We use ax parameter to tell seaborn which subplot to use for this plot
sns.heatmap(data=subset1.corr(), ax=ax1, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset2.corr(), ax=ax2, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset3.corr(), ax=ax3, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset4.corr(), ax=ax4, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset5.corr(), ax=ax5, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})

You should look at matplotlib.add_subplot: 您应该查看matplotlib.add_subplot:

# Here we create a figure instance, and two subplots
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

# We use ax parameter to tell seaborn which subplot to use for this plot
sns.pointplot(x="x", y="y", data=data, ax=ax1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM