简体   繁体   English

带有来自 DataFrame 行的色调的 Seaborn 分类图

[英]Seaborn categorical plot with hue from DataFrame rows

I have this pandas DataFrame:我有这个熊猫数据帧:

>>> print(df)
Channel     0     1     2     3     4     5     6     7
Sample                                                 
7d       3.82  4.10  3.86  3.86  3.95  3.65  3.43  3.63
12d      2.97  4.32  3.50  3.58  3.22  3.37  3.58  3.78
17d      4.01  4.04  4.10  3.43  3.76  3.26  3.35  3.48
DO       3.07  3.58  3.14  3.22  3.11  3.09  3.16  3.16

I want to do a plot similar to this (the code is sns.swarmplot(df) ):我想做一个类似于这个的图(代码是sns.swarmplot(df) ):

在此处输入图片说明

But the colors should be set not per-channel (ie DataFrame column) but per-sample (ie DataFrame rows).但是颜色应该不是按通道(即 DataFrame 列)设置,而是按样本(即 DataFrame 行)设置。 So each "category" on the x-axis will have 4 colors corresponding to the rows 7d, 12d, 17d and DO.因此,x 轴上的每个“类别”将有 4 种颜色,分别对应于 7d、12d、17d 和 DO 行。

Is there an easy way to accomplish this in seaborn?有没有一种简单的方法可以在 seaborn 中实现这一点?

EDIT : I should add that I tried using the hue keyword, but it says it requires using also x and y keyword.编辑:我应该补充一点,我尝试使用hue关键字,但它说它也需要使用xy关键字。 According to this example seems that I need to create a new DataFrame with all numeric values in one column and two other columns with sample and channel information.根据这个例子,我似乎需要创建一个新的 DataFrame ,其中包含一列中的所有数值和其他两列中的样本和通道信息。 Then I can call the plot as sns.swarmplot(x='Channel', y='values', hue='Sample') .然后我可以将绘图称为sns.swarmplot(x='Channel', y='values', hue='Sample') Is there a more direct way that does not involve creating an additional ad-hoc DataFrame?有没有更直接的方法不涉及创建额外的临时数据帧?

EDIT2 : Following @BrenBarn suggestion, I end up creating a new "tidy" DataFrame with: EDIT2 :按照@BrenBarn 的建议,我最终创建了一个新的“整洁”DataFrame:

dd = []
for sa in df.index:
    print(sa)
    d = pd.DataFrame(df.loc[sa]).reset_index()
    d.columns = ['Channel', 'Leakage']
    d['Sample'] = sa
    dd.append(d)
ddf = pd.concat(dd)

And then plotting the data with:然后绘制数据:

sns.swarmplot(x='Channel', y='Leakage', hue='Sample', data=ddf)

which gives the plot I expected:这给出了我预期的情节:

在此处输入图片说明

I was hoping there was a way to tell seaborn to use original "2-D table" format to do the plot which is much more compact and natural for this kind of data.我希望有一种方法可以告诉 seaborn 使用原始的“二维表”格式来绘制对于此类数据来说更加紧凑和自然的图。 If this is possible I would accept the answer ;).如果这是可能的,我会接受答案;)。

You've basically answered your question in the edit, but you may want to look at pd.melt or pd.stack as an easier way of creating your new tidy DataFrame.您基本上已经在编辑中回答了您的问题,但您可能希望将pd.meltpd.stack视为创建新的整洁 DataFrame 的更简单方法。

eg例如

s=df.stack()
s.name='values'
df_tidy=s.reset_index()
sns.stripplot(data=df_tidy,hue='sample',x='Channel',y='values')

or或者

df_tidy=pd.melt(df.reset_index(),id_vars=['sample'],value_vars=df.columns.tolist(),value_name='values')
sns.stripplot(data=df_tidy,hue='sample',x='Channel',y='values')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM