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 set of vertical point plots grouped by a categorical variable with Seaborn
Point plots in Seaborn are used to show point estimates and confidence intervals using scatter plot glyphs. The seaborn.pointplot() function creates these visualizations, and you can group them by categorical variables to compare different categories.
Basic Syntax
The basic syntax for creating a vertical point plot grouped by a categorical variable is ?
import seaborn as sns import pandas as pd import matplotlib.pyplot as plt # Basic syntax sns.pointplot(data=df, x='category_column', y='numeric_column')
Example with Sample Data
Let's create a sample dataset and demonstrate vertical point plots grouped by a categorical variable ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Create sample data
data = {
'Role': ['Batsman', 'Bowler', 'All-rounder', 'Batsman', 'Bowler',
'All-rounder', 'Batsman', 'Bowler', 'All-rounder', 'Batsman'],
'Age': [28, 32, 25, 30, 29, 27, 26, 31, 24, 33],
'Experience': [5, 8, 3, 7, 6, 4, 2, 9, 1, 10]
}
df = pd.DataFrame(data)
print(df.head())
Role Age Experience
0 Batsman 28 5
1 Bowler 32 8
2 All-rounder 25 3
3 Batsman 30 7
4 Bowler 29 6
Creating Vertical Point Plot
Now let's create a vertical point plot showing age grouped by player role ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = {
'Role': ['Batsman', 'Bowler', 'All-rounder', 'Batsman', 'Bowler',
'All-rounder', 'Batsman', 'Bowler', 'All-rounder', 'Batsman'],
'Age': [28, 32, 25, 30, 29, 27, 26, 31, 24, 33]
}
df = pd.DataFrame(data)
# Set the theme
sns.set_theme(style="whitegrid")
# Create vertical point plot grouped by categorical variable
plt.figure(figsize=(8, 6))
sns.pointplot(data=df, x='Role', y='Age')
plt.title('Player Age by Role')
plt.xlabel('Player Role')
plt.ylabel('Age')
plt.show()
Customizing Point Plots
You can customize the appearance of point plots with various parameters ?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Extended sample data
data = {
'Role': ['Batsman', 'Bowler', 'All-rounder'] * 8,
'Age': [28, 32, 25, 30, 29, 27, 26, 31, 24, 33, 27, 30,
29, 28, 26, 32, 25, 31, 28, 29, 30, 27, 26, 33],
'Team': ['A', 'A', 'A', 'B', 'B', 'B'] * 4
}
df = pd.DataFrame(data)
# Customized point plot with hue parameter
plt.figure(figsize=(10, 6))
sns.pointplot(data=df, x='Role', y='Age', hue='Team',
markers=['o', 's'], linestyles=['-', '--'],
palette='Set1')
plt.title('Player Age by Role and Team')
plt.legend(title='Team')
plt.show()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
data |
DataFrame containing the data | data=df |
x |
Categorical variable for grouping | x='Role' |
y |
Numeric variable for point estimates | y='Age' |
hue |
Additional categorical variable for color | hue='Team' |
markers |
Marker styles for different groups | markers=['o', 's'] |
Conclusion
Seaborn's pointplot() function effectively visualizes point estimates and confidence intervals grouped by categorical variables. Use the x parameter for the categorical variable and y for the numeric variable to create clear, informative point plots.
