gpt4 book ai didi

python - 带有群图的 Seaborn PairGrid

转载 作者:行者123 更新时间:2023-11-28 21:40:49 26 4
gpt4 key购买 nike

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

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
...

我想生产一个 Seaborn PairGridstripplot s 抖动或 swarmplot s 用于每对特征 f1f2f3,并使用 value 作为 hue.

对角线上的图应该看起来像这样:

1D strip plot

我用它创建的:

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()

非对角线图将是这样的:

2D strip plot

同样,我用:

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()

因此,我正在尝试的是:

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)

然而,这是屈服的:

Strip plot pair grid

我真的不知道我在这里错过了什么。我仍然可以自己制作情节并将它们放入我自己的子情节中,但这就是配对网格的全部意义所在。由于某种原因,网格不支持这些类型的绘图吗?

最佳答案

与名称所暗示的不同,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()

enter image description here

这里的要点是 PairGridhuestripplothue 完全不同>。您确实可以使用 stripplot 本身的色调来为每个单独的图中的点着色,而 PairGridhue 而是将数据帧分为更多类别,每个类别一个色调值;这是不需要的,因为数据框中的值列包含一个连续变量,您最终会得到与该列中的不同值一样多的类别。

关于python - 带有群图的 Seaborn PairGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45236189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com