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 point plot and control order by passing an explicit order with Seaborn
Point Plot in Seaborn is used to show point estimates and confidence intervals using scatter plot glyphs. The seaborn.pointplot() function creates these visualizations, and you can control the order of categories using the order parameter.
Syntax
seaborn.pointplot(x, y, data, order=None, ...)
Parameters
Key parameters for controlling order:
- x, y: Column names for x and y axes
- data: DataFrame containing the data
- order: List specifying the order of categorical levels
Example with Sample Data
Let's create a point plot with explicit ordering using sample cricket academy data ?
import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Academy': ['Tasmania', 'Victoria', 'South Australia', 'Tasmania', 'Victoria',
'South Australia', 'Tasmania', 'Victoria', 'South Australia'],
'Age': [22, 25, 23, 24, 26, 21, 23, 24, 25]
}
dataFrame = pd.DataFrame(data)
print("Sample Data:")
print(dataFrame.head())
Sample Data:
Academy Age
0 Tasmania 22
1 Victoria 25
2 South Australia 23
3 Tasmania 24
4 Victoria 26
Creating Point Plot with Default Order
First, let's see the default ordering (alphabetical) ?
import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = {
'Academy': ['Tasmania', 'Victoria', 'South Australia', 'Tasmania', 'Victoria',
'South Australia', 'Tasmania', 'Victoria', 'South Australia'],
'Age': [22, 25, 23, 24, 26, 21, 23, 24, 25]
}
dataFrame = pd.DataFrame(data)
sb.set_theme(style="darkgrid")
# Point plot with default order
plt.figure(figsize=(8, 5))
sb.pointplot(x='Academy', y='Age', data=dataFrame)
plt.title('Point Plot - Default Order')
plt.show()
Using Explicit Order Parameter
Now, let's control the order by specifying a custom sequence ?
import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = {
'Academy': ['Tasmania', 'Victoria', 'South Australia', 'Tasmania', 'Victoria',
'South Australia', 'Tasmania', 'Victoria', 'South Australia'],
'Age': [22, 25, 23, 24, 26, 21, 23, 24, 25]
}
dataFrame = pd.DataFrame(data)
sb.set_theme(style="darkgrid")
# Point plot with explicit order
plt.figure(figsize=(8, 5))
sb.pointplot(x='Academy', y='Age', data=dataFrame,
order=["Tasmania", "South Australia", "Victoria"])
plt.title('Point Plot - Custom Order')
plt.show()
Comparison of Ordering Methods
| Method | Order Result | Use Case |
|---|---|---|
| Default (no order parameter) | Alphabetical | Quick visualization |
| Custom order list | User-defined sequence | Logical or importance-based ordering |
| Data-driven order | Based on values (mean, median) | Statistical significance |
Advanced Example with Multiple Categories
You can also use ordering with additional categorical variables ?
import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt
# Extended sample data with hue
data = {
'Academy': ['Tasmania', 'Victoria', 'South Australia'] * 6,
'Age': [22, 25, 23, 24, 26, 21, 23, 24, 25, 26, 27, 22, 25, 24, 26, 23, 25, 24],
'Level': ['Junior', 'Senior'] * 9
}
dataFrame = pd.DataFrame(data)
sb.set_theme(style="darkgrid")
plt.figure(figsize=(10, 6))
sb.pointplot(x='Academy', y='Age', hue='Level', data=dataFrame,
order=["Tasmania", "South Australia", "Victoria"])
plt.title('Point Plot with Hue and Custom Order')
plt.show()
Conclusion
Use the order parameter in seaborn.pointplot() to control the sequence of categories on your plot. This is essential for creating logical, meaningful visualizations that emphasize relationships in your data.
