Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a Swarm Plot with Matplotlib?
A swarm plot is a categorical scatter plot that shows the distribution of data points without overlap. In Python, we can create swarm plots using Seaborn library, which provides better categorical plotting capabilities than Matplotlib alone.
Basic Swarm Plot
Let's start with a simple swarm plot using Seaborn ?
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [8, 5]
plt.rcParams["figure.autolayout"] = True
# Create sample data
data = pd.DataFrame({
"Category": ["A", "A", "A", "B", "B", "B", "C", "C", "C"],
"Values": [1, 2, 3, 2, 4, 5, 3, 5, 6]
})
# Create swarm plot
sns.swarmplot(x="Category", y="Values", data=data)
plt.title("Basic Swarm Plot")
plt.show()
Swarm Plot with Box Plot Overlay
You can combine a swarm plot with a box plot to show both individual data points and statistical summaries ?
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
plt.rcParams["figure.figsize"] = [8, 5]
plt.rcParams["figure.autolayout"] = True
# Create more complex dataset
np.random.seed(42)
categories = ["Group A", "Group B", "Group C"]
data = []
for cat in categories:
values = np.random.normal(loc=np.random.randint(10, 50), scale=5, size=20)
for val in values:
data.append({"Category": cat, "Values": val})
df = pd.DataFrame(data)
# Create swarm plot
ax = sns.swarmplot(x="Category", y="Values", data=df, alpha=0.7)
# Add box plot overlay
sns.boxplot(x="Category", y="Values", data=df,
showcaps=False,
boxprops={'facecolor': 'None', 'linewidth': 2},
showfliers=False,
whiskerprops={'linewidth': 2},
ax=ax)
plt.title("Swarm Plot with Box Plot Overlay")
plt.ylabel("Values")
plt.show()
Customized Swarm Plot
You can customize colors, size, and styling of swarm plots ?
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True
# Create dataset with additional grouping variable
np.random.seed(42)
data = []
for category in ["Low", "Medium", "High"]:
for condition in ["Control", "Treatment"]:
values = np.random.normal(
loc={"Low": 20, "Medium": 35, "High": 50}[category] +
(5 if condition == "Treatment" else 0),
scale=8,
size=15
)
for val in values:
data.append({
"Category": category,
"Condition": condition,
"Score": val
})
df = pd.DataFrame(data)
# Create customized swarm plot
plt.figure(figsize=(10, 6))
sns.swarmplot(x="Category", y="Score", hue="Condition",
data=df, palette=["lightblue", "orange"],
size=6, alpha=0.8)
plt.title("Customized Swarm Plot with Grouping", fontsize=16)
plt.xlabel("Category", fontsize=12)
plt.ylabel("Score", fontsize=12)
plt.legend(title="Condition")
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
x, y |
Column names for categorical and numerical data | "Category", "Values" |
hue |
Additional grouping variable | "Condition" |
size |
Size of the markers | 5, 8, 10 |
alpha |
Transparency of points | 0.5, 0.7, 1.0 |
palette |
Color scheme | "viridis", ["red", "blue"] |
Conclusion
Swarm plots are excellent for visualizing categorical data distributions while showing individual data points. Use Seaborn's swarmplot() for better categorical plotting than pure Matplotlib, and combine with box plots for comprehensive data visualization.
