简体   繁体   English

Seaborn swarmplot的分组数据帧

[英]Seaborn swarmplot of grouped dataframe

When I have a dataframe likes this here: 当我有一个数据帧时喜欢这里:

import pandas as pd
import seaborn as sns
import random

random.seed(0)

df = pd.DataFrame({"Data":[random.random() for i in range(100)], "Cluster":[random.randint(0,10) for i in range(100)]})

I can easily plot the clusters with seaborn as boxplots: 我可以很容易地用seaborn绘制簇作为箱线图:

sns.boxplot(df["Data"], groupby=df["Cluster"])

Which gives me something like this: 这给了我这样的东西: 在此输入图像描述

Unfortunately seaborn swarmplot does not have a groupby keyword. 不幸的是,seaborn swarmplot没有groupby关键字。 So, how can I convinently plot this dataframe as grouped swarmplots? 那么,我怎样才能将这个数据框架作为分组的swarmplots? I want the same figure as shown only with swarmplots instead of boxplots. 我想要的只是用swarmplots而不是boxplots显示的相同数字。 I played around with the groupby object itself: 我玩了groupby对象本身:

df.groupby(by="Cluster")

So far I could not convince seaborn to accept it. 到目前为止,我无法说服seaborn接受它。

I don't think you need to groupby - you simply want to specify the cluster as your x value and the data as your y: 我不认为你需要groupby - 你只想将集群指定为x值,将数据指定为y:

sns.swarmplot(data=df, x="Cluster", y="Data")

Gives you: 给你:

在seaborn的群体情节

However, if you had a further category that you wanted to colour by, eg 但是,如果你有另一个类别,你想要着色,例如

df = pd.DataFrame({"Data":[random.random() for _ in range(100)],
                   "Cluster":[random.randint(0,10) for _ in range(100)],
                   "Category": [random.choice(("A", "B")) for _ in range(100)]})

You can then use the hue argument like so: 然后你可以像这样使用hue参数:

sns.swarmplot(data=df, x="Cluster", y="Data", hue="Category")

and get: 得到:

与类别的群体情节

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

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