Swarm plot

Seaborn basics

2 min read

Published Oct 7 2025


24
0
0
0

ChartsGraphsMatplotlibNumPyPandasPythonSeabornVisualisation

seaborn.swarmplot() is used to display individual data points for one or more categorical variables, similar to sns.stripplot().
However, unlike stripplot, it automatically arranges (swarm) the points so that they don’t overlap — creating a cleaner, more readable visualisation.

You can think of it as a “smarter” stripplot that spreads points apart along the categorical axis while keeping their exact numeric values.


Syntax:

sns.swarmplot(
    data=None,
    x=None,
    y=None,
    hue=None,
    order=None,
    hue_order=None,
    dodge=False,
    orient=None,
    color=None,
    palette=None,
    size=5,
    marker='o',
    linewidth=0,
    edgecolor=None,
    alpha=None,
    **kwargs
)

Parameters:

  • data = DataFrame containing the data
  • x, y = Categorical and numerical variables
  • hue = Adds subgroups with different colours
  • order, hue_order = Category/subcategory order
  • palette = Colour scheme
  • size = Dot size
  • alpha = Transparency (0–1)
  • marker = Shape of the dots (e.g., 'o', 's', '^')
  • dodge = Places hue subgroups side by side
  • orient = "v" (vertical) or "h" (horizontal)
  • linewidth, edgecolor = Outline style for dots




Basic example

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.swarmplot(data=tips, x="day", y="total_bill")
plt.show()

Each dot = one observation of total_bill on a specific day. Dots are evenly spaced, so none overlap.


seaborn swarm plot basic example





Add hue (subgroups)

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.swarmplot(data=tips, x="day", y="total_bill", hue="sex", palette="Set2")
plt.show()

  • Dots are coloured by the sex column.
  • Each day now has two overlapping but neatly separated swarms.

seaborn swarm plot hue example





Separate subgroups (dodge)

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.swarmplot(data=tips, x="day", y="total_bill", hue="sex", dodge=True, palette="pastel")
plt.show()

Dots for different hue categories appear side by side (not overlapping).


seaborn swarm plot dodge example





Horizontal swarmplot

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.swarmplot(data=tips, x="total_bill", y="day", orient="h")
plt.show()

Same data — just flipped horizontally. Useful when category names are long or when numeric range fits better on the x-axis.


seaborn swarm plot horizontal example





Customise style

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

sns.swarmplot(
    data=tips,
    x="day",
    y="total_bill",
    hue="sex",
    dodge=True,
    size=6,
    palette="coolwarm",
    marker="D",
    alpha=0.7,
    linewidth=0.5,
    edgecolor="black"
)
plt.show()

  • size → dot size
  • marker → shape ("o", "s", "D", "^", etc.)
  • alpha → transparency
  • edgecolor → outline colour

seaborn swarm plot style example
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact