pandas 使用 Seaborn FacetGrid 绘制相关热图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29592306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:11:22  来源:igfitidea点击:

Plotting correlation heatmaps with Seaborn FacetGrid

pythonpandasplotseaborn

提问by fakedrake

I am trying to create a single image with heatmaps representing the correlation of features of data points for each label separately. With seaborn I can create a heatmap for a single class like so

我正在尝试创建一个带有热图的单个图像,分别表示每个标签的数据点特征的相关性。使用 seaborn,我可以像这样为单个类创建热图

grouped = df.groupby('target')
sns.heatmap(grouped.get_group('Class_1').corr())

An I get this which makes sense:

我明白这是有道理的:

Class_1 correlation heatmap

Class_1 相关热图

But then I try to make a list of all the labels like so:

但后来我尝试列出所有标签,如下所示:

g = sns.FacetGrid(df, col='target')
g.map(lambda grp: sns.heatmap(grp.corr()))

And sadly I get this which makes no sense to me:

可悲的是,我明白了这对我来说毫无意义:

Failing attempt to plot all classes

尝试绘制所有类失败

回答by fakedrake

Turns out you can do it pretty conciesely with just seaborn if you use map_dataframeinstead of map.

事实证明,如果您使用map_dataframe而不是map.

g = sns.FacetGrid(df, col='target')
g.map_dataframe(lambda data, color: sns.heatmap(data.corr(), linewidths=0))

@mwaskom points out in his comment that it might be a good idea to explicitly set the limits of the colormap so that the different facets can be more directly compared. The documentationdescribes relevant heatmapparameters:

@mwaskom 在他的评论中指出,明确设置颜色图的限制可能是一个好主意,以便可以更直接地比较不同的方面。该文档描述的相关heatmap参数:

vmin, vmax: floats, optional

Values to anchor the colormap, otherwise they are inferred from the data and other keyword arguments. When a diverging dataset is inferred, one of these values may be ignored.

vminvmax:浮点数,可选

锚定颜色图的值,否则它们是从数据和其他关键字参数推断出来的。当推断出发散数据集时,可能会忽略这些值之一。

回答by cphlewis

Without FacetGrid, but making a corr heatmap for each group in a column:

没有 FacetGrid,但为列中的每个组制作 corr 热图:

import pandas as pd
import seaborn as sns
from numpy.random import randint
import matplotlib.pyplot as plt


df = pd.DataFrame(randint(0,10,(200,12)),columns=list('abcdefghijkl'))
grouped = df.groupby('a')
rowlength = grouped.ngroups/2 # fix up if odd number of groups
fig, axs = plt.subplots(figsize=(9,4), nrows=2, ncols=rowlength)

targets = zip(grouped.groups.keys(), axs.flatten())
for i, (key, ax) in enumerate(targets):
    sns.heatmap(grouped.get_group(key).corr(), ax=ax,
                xticklabels=(i >= rowlength),
                yticklabels=(i%rowlength==0),
                cbar=False) # Use cbar_ax into single side axis
    ax.set_title('a=%d'%key)
plt.show()

enter image description hereMaybe there's a way to set up a lambda to correctly pass the data from the g.facet_data()generator through corrbefore going to heatmap.

在此处输入图片说明也许有一个办法λ设定了正确传递从数据 g.facet_data()通过发电机corr才去heatmap