Sorting individual points in seaborn swarmplot

93 Views Asked by At

Is there a way to change the order of the individual dots in a seaborn swarmplot within the categories? That is, I would like to sort them so that all points of the same color are bunched together and we can group them in the order blue/orange/green.

Example:

  • In the figure below, Male/Thursday has: orange/blue/green/blue.
  • Can we sort the points so that they appear as blue/blue/orange/green?
import seaborn as sns
# Load data
tips = sns.load_dataset("tips")

# Simplify the data
df = tips.loc[tips["size"] > 3]

# Plot
sns.swarmplot(data=df, x="sex", y="day", hue="size", size = 8) 

enter image description here

1

There are 1 best solutions below

0
My Work On

You could use dodge as the docs say:

By default, the different levels of the hue variable are intermingled in each swarm, but setting dodge=True will split them:

import seaborn as sns
# Load data
tips = sns.load_dataset("tips")

# Simplify the data
df = tips.loc[tips["size"] > 3]

# Plot
sns.swarmplot(data=df, x="sex", y="day", hue="size", size = 8, dodge=True) 

enter image description here