Beeswarm

logo of a chart:Beeswarm2Big

A beeswarm plot or swarmplot is a type of data visualization that displays individual data points in a way that they don't overlap, resulting in a "swarming" effect that resembles a swarm of bees.

This chart type helps in revealing the distribution of the data along a numeric variable, highlighting the density and variation of the data more effectively than traditional scatter plots or box plots.

This section provides many beeswarm chart examples made with Python, using seaborn or matplotlib. Examples cover the most common use-cases, but also show how far in term of customization it is possible to go.

🤔 When to use a beeswarm chart?

The beeswarm chart is a very good alternative to the boxplot that hides the underlying dataset.

However, do not try to use it with a very big dataset. Indeed, avoiding circle overlap will be possible with a small amount of data points, but it will get impossible otherwise. In this case, take a look to the violin chart instead.

To read more about this, visit data-to-viz.com that has a dedicated article.



Seaborn logoBeeswarm with Seaborn

Seaborn is a python library allowing to make better charts easily. The swarmplot() function should get you started in minutes. The examples below aim at showcasing the various possibilities this function offers.

🔎 swarmplot() function parameters→ see full doc

→ Description

The swarmplot() function from seaborn creates a categorical scatter plot where each point is adjusted (swarmed) so that it does not overlap with others. This plot is particularly useful for visualizing the distribution of data points in small datasets while avoiding data occlusion.

→ Arguments

Description

Variable name that specify which column to plot on the x-axis.

Possible values → string

The simplest usage is to specify a column name (string) of a numerical variable in data. Alternatively, you can omit data and pass a vector-like object of numerical values to x.

Code Example

import seaborn as sns
import matplotlib.pyplot as plt

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

Matplotlib logoBeeswarm with Matplotlib

Building a beeswarm plot with matplotlib requires more manual work since no built-in function exists for the job.

The following blog-post explains how to build yours from scratch. The function divides the data into bins, calculates the upper bounds for each bin, and then arranges the data points in each bin to create the horizontal spread in the plot, ensuring they don't overlap.

Once the dot position is available, it is possible to render it usingmatplotlib:

Contact


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥