F. Jehn
F. Jehn

Reputation: 377

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:

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

Which gives me something like this: enter image description here

Unfortunately seaborn swarmplot does not have a groupby keyword. So, how can I convinently plot this dataframe as grouped swarmplots? I want the same figure as shown only with swarmplots instead of boxplots. I played around with the groupby object itself:

df.groupby(by="Cluster")

So far I could not convince seaborn to accept it.

Upvotes: 1

Views: 1477

Answers (1)

asongtoruin
asongtoruin

Reputation: 10359

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:

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

Gives you:

swarm plot in seaborn

However, if you had a further category that you wanted to colour by, e.g.

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:

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

and get:

swarm plot with categories

Upvotes: 3

Related Questions