Keep points from overlapping with a swarm plot

6.2.9

Keep points from overlapping with a swarm plot

Last updated 2020-05-20 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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()

plt.show()

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.
  • Violin Plot — Show distribution shape with box plot + KDE
  • Box Plot — Compare median, quartiles, and outliers
  • Rug Plot — Display individual data points as short lines along the axis