python - 使用 Seaborn FacetGrid 绘制相关热图

标签 python pandas plot seaborn

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

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

我明白这是有道理的:

Class_1 correlation heatmap

然后我尝试像这样列出所有标签:

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

可悲的是,我得到了这个对我来说毫无意义的东西:

Failing attempt to plot all classes

最佳答案

事实证明,如果您使用 map_dataframe 而不是 map,您只需使用 seaborn 就可以非常简洁地完成此操作:

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

@mwaskom 在他的评论中指出,显式设置颜色图的限制可能是个好主意,这样可以更直接地比较不同的方面。 documentation描述相关的heatmap参数:

vmin, vmax : floats, optional

Values to anchor the colormap, otherwise they are inferred from the data and other keyword arguments.

关于python - 使用 Seaborn FacetGrid 绘制相关热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29592306/

相关文章:

python 语法错误: invalid syntax %matplotlib inline

python - 使用悬停工具在 Bokeh 中绘制交互式散点图

python - 如何在匹配值(列表)包含 X 的字典中找到键?

Python argparse : name parameters

python - 如何使用 Pandas 将 QTableWidget 数据导出到 Excel?

python - Seaborn 条形图中 X 轴上的日期排序和格式化

python - iPython notebook - 在子图次要 y 轴上设置 ylim

python - 类型错误 - Python Simpy

python - PEP 8 : E128 sometimes requires spaces and sometimes does not

python - 在 Pandas 中用自身的功能替换列?