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 Bar Plot and control swarm order by passing an explicit order with Seaborn
Bar plots in Seaborn display point estimates and confidence intervals as rectangular bars. The seaborn.barplot() function allows you to control the ordering of categories by passing an explicit order using the order parameter.
Importing Required Libraries
First, import the necessary libraries for data visualization ?
import seaborn as sb import pandas as pd import matplotlib.pyplot as plt
Creating Sample Data
Let's create a sample dataset to demonstrate bar plot ordering ?
# Create sample cricket data
data = {
'Academy': ['Victoria', 'Western Australia', 'South Australia', 'Tasmania',
'Victoria', 'Western Australia', 'South Australia', 'Tasmania'],
'Matches': [25, 30, 20, 15, 28, 32, 18, 12]
}
dataFrame = pd.DataFrame(data)
print(dataFrame)
Academy Matches
0 Victoria 25
1 Western Australia 30
2 South Australia 20
3 Tasmania 15
4 Victoria 28
5 Western Australia 32
6 South Australia 18
7 Tasmania 12
Basic Bar Plot
Create a basic bar plot without specifying order ?
import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Academy': ['Victoria', 'Western Australia', 'South Australia', 'Tasmania',
'Victoria', 'Western Australia', 'South Australia', 'Tasmania'],
'Matches': [25, 30, 20, 15, 28, 32, 18, 12]
}
dataFrame = pd.DataFrame(data)
# Basic bar plot
plt.figure(figsize=(10, 6))
sb.barplot(data=dataFrame, x='Academy', y='Matches')
plt.title('Cricket Matches by Academy (Default Order)')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Controlling Order with order Parameter
Control the display order by passing an explicit order to the order parameter ?
import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Academy': ['Victoria', 'Western Australia', 'South Australia', 'Tasmania',
'Victoria', 'Western Australia', 'South Australia', 'Tasmania'],
'Matches': [25, 30, 20, 15, 28, 32, 18, 12]
}
dataFrame = pd.DataFrame(data)
# Bar plot with custom order
plt.figure(figsize=(10, 6))
custom_order = ['Victoria', 'Western Australia', 'South Australia', 'Tasmania']
sb.barplot(data=dataFrame, x='Academy', y='Matches', order=custom_order)
plt.title('Cricket Matches by Academy (Custom Order)')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Ordering by Values
You can also order bars by their values (ascending or descending) ?
import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Academy': ['Victoria', 'Western Australia', 'South Australia', 'Tasmania',
'Victoria', 'Western Australia', 'South Australia', 'Tasmania'],
'Matches': [25, 30, 20, 15, 28, 32, 18, 12]
}
dataFrame = pd.DataFrame(data)
# Calculate mean matches per academy and sort
academy_order = dataFrame.groupby('Academy')['Matches'].mean().sort_values(ascending=False).index
# Bar plot ordered by values (descending)
plt.figure(figsize=(10, 6))
sb.barplot(data=dataFrame, x='Academy', y='Matches', order=academy_order)
plt.title('Cricket Matches by Academy (Ordered by Mean Matches)')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Key Parameters
The order parameter accepts a list specifying the desired category order ?
| Parameter | Description | Example |
|---|---|---|
order |
List of category names in desired order | ['A', 'B', 'C'] |
x |
Column for x-axis categories | 'Academy' |
y |
Column for y-axis values | 'Matches' |
Conclusion
Use the order parameter in seaborn.barplot() to control category ordering. You can specify custom order lists or sort categories by their values for better data visualization.
