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
Python Pandas - Draw a swarm plot and control swarm order by passing an explicit order with Seaborn
A swarm plot in Seaborn creates a categorical scatterplot with non-overlapping points, making it ideal for visualizing the distribution of values across categories. You can control the order of categories using the order parameter to customize how data appears on the plot.
Creating Sample Data
Let's create sample cricket data to demonstrate swarm plot ordering ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create sample cricket data
data = {
'Academy': ['Victoria', 'Western Australia', 'South Australia', 'Victoria',
'Western Australia', 'South Australia', 'Victoria', 'Western Australia',
'South Australia', 'Victoria', 'Western Australia', 'South Australia'],
'Matches': [25, 30, 15, 40, 35, 20, 28, 45, 18, 32, 38, 22]
}
df = pd.DataFrame(data)
print(df.head())
Academy Matches
0 Victoria 25
1 Western Australia 30
2 South Australia 15
3 Victoria 40
4 Western Australia 35
Basic Swarm Plot
First, let's create a basic swarm plot without specifying order ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Academy': ['Victoria', 'Western Australia', 'South Australia'] * 4,
'Matches': [25, 30, 15, 40, 35, 20, 28, 45, 18, 32, 38, 22]
}
df = pd.DataFrame(data)
# Create basic swarm plot
plt.figure(figsize=(8, 5))
sns.swarmplot(data=df, x='Academy', y='Matches')
plt.title('Basic Swarm Plot')
plt.show()
Controlling Swarm Order
Now let's control the order of academies using the order parameter ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Academy': ['Victoria', 'Western Australia', 'South Australia'] * 4,
'Matches': [25, 30, 15, 40, 35, 20, 28, 45, 18, 32, 38, 22]
}
df = pd.DataFrame(data)
# Create swarm plot with custom order
plt.figure(figsize=(8, 5))
sns.swarmplot(data=df, x='Academy', y='Matches',
order=['Victoria', 'Western Australia', 'South Australia'])
plt.title('Swarm Plot with Custom Order')
plt.show()
Enhancing the Plot
You can further customize the swarm plot with colors and styling ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data with additional column for color mapping
data = {
'Academy': ['Victoria', 'Western Australia', 'South Australia'] * 4,
'Matches': [25, 30, 15, 40, 35, 20, 28, 45, 18, 32, 38, 22],
'Performance': ['Good', 'Excellent', 'Average', 'Excellent', 'Good', 'Average',
'Good', 'Excellent', 'Average', 'Good', 'Excellent', 'Average']
}
df = pd.DataFrame(data)
# Enhanced swarm plot
plt.figure(figsize=(10, 6))
sns.set_theme(style="whitegrid")
sns.swarmplot(data=df, x='Academy', y='Matches', hue='Performance',
order=['Victoria', 'Western Australia', 'South Australia'],
palette='viridis')
plt.title('Enhanced Swarm Plot with Custom Order and Colors')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
order |
Controls category order | ['A', 'B', 'C'] |
hue |
Color mapping variable | 'Performance' |
palette |
Color scheme | 'viridis', 'Set1' |
size |
Point size | 5, 8, 10 |
Conclusion
Use the order parameter in sns.swarmplot() to control category display order. This is particularly useful for logical ordering like performance rankings or chronological sequences.
