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
Selected Reading
Python Pandas - Draw a boxplot and control box order by passing an explicit order with Seaborn
A box plot in Seaborn visualizes data distributions across categories. The seaborn.boxplot() function creates these plots, and you can control the order of categories using the order parameter.
Basic Box Plot Syntax
The basic syntax for creating a box plot with custom ordering ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Category': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'],
'Values': [23, 45, 56, 78, 32, 87, 65, 43, 29]
}
df = pd.DataFrame(data)
sns.boxplot(x='Category', y='Values', data=df, order=['C', 'A', 'B'])
plt.title('Box Plot with Custom Order')
plt.show()
Example with Cricket Data
Let's create a more comprehensive example using sample cricket academy data ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create sample cricket academy data
data = {
'Academy': ['Tasmania', 'Victoria', 'South Australia', 'Tasmania',
'Victoria', 'South Australia', 'Tasmania', 'Victoria'],
'Age': [22, 25, 28, 24, 27, 23, 26, 29]
}
df = pd.DataFrame(data)
# Create box plot with explicit order
sns.boxplot(x='Academy', y='Age', data=df,
order=['Tasmania', 'South Australia', 'Victoria'])
plt.title('Cricket Player Ages by Academy (Custom Order)')
plt.xlabel('Academy')
plt.ylabel('Age')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Parameters
| Parameter | Description | Example |
|---|---|---|
x |
Column for x-axis categories | 'Academy' |
y |
Column for y-axis values | 'Age' |
data |
DataFrame containing the data | df |
order |
List specifying category order | ['A', 'C', 'B'] |
Multiple Ordering Examples
You can also control ordering based on statistical measures ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data with more variation
data = {
'Team': ['Red', 'Blue', 'Green', 'Red', 'Blue', 'Green'] * 4,
'Score': [85, 92, 78, 88, 95, 82, 90, 89, 76, 87, 94, 80,
86, 91, 79, 89, 93, 81, 84, 90, 77, 85, 96, 83]
}
df = pd.DataFrame(data)
# Order by team name alphabetically
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
sns.boxplot(x='Team', y='Score', data=df, order=['Blue', 'Green', 'Red'])
plt.title('Custom Alphabetical Order')
plt.subplot(1, 2, 2)
# Order by median score (calculate first)
median_order = df.groupby('Team')['Score'].median().sort_values().index.tolist()
sns.boxplot(x='Team', y='Score', data=df, order=median_order)
plt.title('Order by Median Score')
plt.tight_layout()
plt.show()
Conclusion
Use the order parameter in seaborn.boxplot() to control category sequence. This helps create more meaningful visualizations by arranging categories logically or by statistical measures.
Advertisements
