是否有一种方法只能将色相应用于Seaborn中的PairGrid的下部 [英] Is there a way to apply hue ONLY to lower part of PairGrid in seaborn

查看:100
本文介绍了是否有一种方法只能将色相应用于Seaborn中的PairGrid的下部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将色相仅应用于海洋型 PairGrid 的下部。

I was wondering if it was possible to apply hue to only the lower part of a seaborn PairGrid.

例如,假设我有下图:

For example, say I have the following figure:

对于我需要呈现的内容,我d希望将密度图保留在对角线上,将整体散布图保留在上方(上面印有相关系数,我知道该怎么做),但是在下方,我想通过色相将点分开以显示我的观众,如果我们对数据进行子集处理,会发生什么情况。

For what I need to present I'd like to keep the density plots on the diagonal, the overall scatter plots on the upper (with printed correlation coefficients above them which I know how to do), but on the lower I want to split the points up by hue just to show my audience what happens if we do subset the data.

我想过只是找出鞋面的相关性,做一个色相图,然后改变所有的标记。上部的地块使用相同的颜色,但随后我失去了对角线的密度。

I thought about just finding out the correlations for the upper, doing a hue plot and just changing all of the markers in the upper plots to the same colour, but then I lose the densities on my diagonal.

有人知道我的问题是否可能吗?

Does anybody know if my problem is possible?

我正在使用的当前代码是

Current code I'm using is

ff = sns.PairGrid(test2,vars = ['OzekePower','Power0','Power1','Power2'],palette = husl )
ff.map_upper(sns.scatterplot)
ff.map_lower(sns.scatterplot)
ff.map_diag(sns.kdeplot)

所以我想要的是类似
ff.map_lower(sns.scatterplot,hue ='species')
,但这会产生错误。

So what I'm hoping for is something like ff.map_lower(sns.scatterplot,hue='species') but this yields an error.

编辑-如果我将诊断和上部留空并分别分配给空图,则可以这样做,但这似乎是

EDIT - I can do it if I leave the diag and upper blank and assign to the empty plots individually but this seems a lot more lengthy.

推荐答案

不幸的是, PairGrid 没有 map_dataframe 方法,否则可以用于在映射中包括其他数据框列。仅在 PairGrid 的下部获得色相的一种破解方法是离开 PairGrid 创建中输入> hue 参数,并在不需要色相的地方填充网格。

然后手动设置手动将色相的必需参数添加到网格,最后调用 map_lower ,然后将看到网格,就好像从一开始就指定了色调

Unfortunately, PairGrid does not have a map_dataframe method, which could otherwise be used to include further dataframe columns into the mapping. Kind of a hack to obtain hue only in the lower part of the PairGrid is to leave the hue argument out in the PairGrid creation, and fill the grid where no hue is desired.
Then manually set the required parameters for the hue manually to the grid and finally call map_lower, which will then see the grid as if it had hue specified from the start.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset("iris", cache=True)

g = sns.PairGrid(df)

g.map_upper(sns.scatterplot)

g.map_diag(sns.kdeplot)

# Now set parameters needed for `hue`
g.hue_vals = df["species"]
g.hue_names = df["species"].unique()
g.palette = sns.color_palette("husl", len(g.hue_names))
# Then map lower
g.map_lower(sns.scatterplot)

plt.show()

这篇关于是否有一种方法只能将色相应用于Seaborn中的PairGrid的下部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆