Keep points from overlapping with a swarm plot

Visualize

Keep points from overlapping with a swarm plot

Created: Last updated: Read time: 1 min

A swarm plot offsets each observation to avoid overlap, so you can retain individual values while still seeing the overall shape of the distribution.

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

fig, ax = plt.subplots(figsize=(6, 4))
sns.swarmplot(data=tips, x="day", y="total_bill", hue="sex", dodge=True, ax=ax)

ax.set_xlabel("Day of week")
ax.set_ylabel("Bill total ($)")
ax.set_title("Bill totals by weekday (swarm plot)")
ax.grid(axis="y", alpha=0.2)

fig.tight_layout()
fig.savefig("static/images/visualize/distribution/swarmplot.svg")

The staggered dots reveal both distribution shape and individual outliers.

Reading tips #

  • The vertical extent of each stack corresponds to local density, and outliers remain visible as individual points.
  • With very large data sets, swarm plots become heavy to compute, so sampling or adjusting size may be necessary.
  • Setting dodge=True separates each hue category into its own column for easier comparisons.