具有群图的Seaborn PairGrid

jde*_*esa 2 python data-visualization seaborn

我有一个像这样的数据集(kinda):

f1  f2  f3     value
4   2   3      0.927252
1   3   0      0.153415
0   1   1      0.928820
1   0   4      0.933250
0   4   3      0.397307
...
Run Code Online (Sandbox Code Playgroud)

我想,以产生Seaborn PairGridstripplots的抖动或swarmplotS代表每个对特征f1f2f3,并使用valuehue

对角线上的图应如下所示:

一维条形图

我创建的:

df = ...  # My dataset
sns.stripplot("f1", "f1", "value", data=df, jitter=True,
              palette=sns.light_palette("red", len(df)),
              hue_order=sorted(df["value"])).legend().remove()
Run Code Online (Sandbox Code Playgroud)

非对角线图将是这样的:

2D带状图

同样,我用以下方法制作的:

df = ...  # My dataset
sns.stripplot("f1", "f2", "value", data=df, jitter=True,
              palette=sns.light_palette("red", len(df)),
              hue_order=sorted(df["value"])).legend().remove()
Run Code Online (Sandbox Code Playgroud)

因此,我正在尝试的是:

import seaborn as sns
df = ...  # My dataset
g = sns.PairGrid(df, hue="value", palette=sns.light_palette("red", len(df)),
                 hue_order=sorted(df["value"]), vars=df.columns[:-1])
g.map_diag(lambda x, **kwargs: sns.stripplot(x, x, **kwargs), jitter=True)
g.map_offdiag(sns.stripplot, jitter=True)
Run Code Online (Sandbox Code Playgroud)

但是,这产生了:

带状图对网格

我真的不知道我在这里想念的是什么。我仍然可以自己绘制情节并将其放入自己的子图中,但这就是配对网格的全部要点。由于某种原因,网格上不支持这些样地吗?

Imp*_*est 5

与名称可能暗示的不同,该hue参数未定义颜色。最好将其视为“更深的维度”或类似内容。尽管在许多情况下,此进一步的尺寸可以通过颜色显示,但不一定对每个图都适用。

为了获得所需的PairGrid,我们可以省略色相,以便显示所有值。

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,5, size=(4**3, 3)), columns=["f1", "f2", "f3"])
df["value"] = np.random.rand(len(df))

g = sns.PairGrid(df, vars=df.columns[:-1])
g.map(sns.stripplot, jitter=True, size=3)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这里的关键是,在huePairGrid东西比完全不同huestripplot。事实上,你可以使用stripplot本身的色调在每一个人的情节上色点,同时huePairGrid,而划分数据框到其他类别,每个色调值一类; 这在这里是不需要的,因为数据框中的value列包含一个连续变量,您最终将获得与该列中的不同值一样多的类别。