[英]Seaborn PairGrid with swarm plots
I have a dataset (kinda) like this:我有一个这样的数据集(有点):
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
...
I want to produce a Seaborn PairGrid
with stripplot
s with jitter or swarmplot
s for each pair of features f1
, f2
and f3
, and use value
for the hue
.我想为每对特征
f1
、 f2
和f3
生成一个带有带抖动或swarmplot
的stripplot
s 的 Seaborn PairGrid
,并使用hue
value
。
Plots in the diagonals should look something like this:对角线上的图应该是这样的:
Which I created with:我创建的:
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()
And off-diagonal plots would be like this:非对角线图将是这样的:
Which, likewise, I made with:同样,我用以下方法制作:
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()
What I'm trying, therefore, is:因此,我正在尝试的是:
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)
However, this is yielding:然而,这正在产生:
I don't really know what I'm missing here.我真的不知道我在这里错过了什么。 I can still make the plots my self and put them into my own subplots, but that's the whole point of the pair grid.
我仍然可以自己制作情节并将它们放入我自己的子情节中,但这就是配对网格的全部意义。 Are these kinds of plots not supported on a grid for some reason?
出于某种原因,网格不支持这些类型的图吗?
Unlike the name may suggest, the hue
parameter does not define a color.与名称所暗示的不同,
hue
参数不定义颜色。 It may be better to think of it as something like "further dimension" or similar.将其视为“更远维度”或类似的东西可能会更好。 While in many cases this further dimension is visualized by color, it is not necessarily true for every plot.
虽然在许多情况下,这个进一步的维度是通过颜色可视化的,但不一定适用于每个图。
In order to get the desired PairGrid, we may leave the hue out, such that all values are shown.为了获得所需的 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()
The point here is that the hue
of the PairGrid
is something completely different than the hue
of the stripplot
.这里的关键是,在
hue
的的PairGrid
是东西比完全不同hue
的的stripplot
。 You may indeed use the hue of the stripplot itself to colorize the points in each individual plot, while the hue
of the PairGrid
rather divides the dataframe into further categories, one category per hue value;您确实可以使用带状图本身的色调来为每个单独的图中的点着色,而
PairGrid
的hue
则将数据帧分为更多类别,每个色调值一个类别; this is unwanted here, because the value column in the dataframe contains a continuous variable and you would end up with as many categories as different values in that column.这在这里是不需要的,因为数据框中的值列包含一个连续变量,您最终会得到与该列中不同值一样多的类别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.