5 min read min read
Seaborn Statistical Plots
Learn to create statistical visualizations with Seaborn
Seaborn Statistical Plots
What Makes Seaborn Special?
Seaborn has plots designed for statistics:
- Show distributions
- Show relationships
- Show comparisons
Distribution Plots
Histogram with Density
code.py
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
data = np.random.normal(50, 10, 500)
sns.histplot(data, kde=True) # kde = smooth line
plt.show()KDE Plot (Smooth Distribution)
code.py
sns.kdeplot(data)
plt.show()Compare Distributions
code.py
import pandas as pd
df = pd.DataFrame({
'Score': [60, 65, 70, 75, 80, 50, 55, 60, 65, 70],
'Group': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B']
})
sns.kdeplot(data=df, x='Score', hue='Group')
plt.show()Violin Plot
Like box plot, but shows distribution shape:
code.py
sns.violinplot(data=df, x='Group', y='Score')
plt.show()Relationship Plots
Regression Plot
Shows scatter with trend line:
code.py
df = pd.DataFrame({
'Hours': [1, 2, 3, 4, 5, 6, 7],
'Score': [50, 55, 60, 65, 75, 80, 85]
})
sns.regplot(data=df, x='Hours', y='Score')
plt.show()Automatically adds best-fit line!
Pair Plot
See all relationships at once:
code.py
df = pd.DataFrame({
'Age': [25, 30, 35, 40, 45],
'Salary': [40000, 50000, 60000, 70000, 80000],
'Experience': [2, 5, 8, 12, 15]
})
sns.pairplot(df)
plt.show()Creates scatter plots for every pair of columns!
Count Plot
Count categories:
code.py
df = pd.DataFrame({
'City': ['NYC', 'LA', 'NYC', 'Chicago', 'NYC', 'LA']
})
sns.countplot(data=df, x='City')
plt.show()Heatmap
Show correlation matrix:
code.py
df = pd.DataFrame({
'Age': [25, 30, 35, 40, 45],
'Salary': [40000, 50000, 60000, 70000, 80000],
'Experience': [2, 5, 8, 12, 15]
})
corr = df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()annot=True shows numbers in cells.
Strip Plot and Swarm Plot
Show individual points:
code.py
# Strip plot (points may overlap)
sns.stripplot(data=df, x='Group', y='Score')
# Swarm plot (points don't overlap)
sns.swarmplot(data=df, x='Group', y='Score')Joint Plot
Scatter + histograms together:
code.py
sns.jointplot(data=df, x='Age', y='Salary')
plt.show()Complete Example
code.py
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
# Sample data
df = pd.DataFrame({
'Department': ['Sales']*30 + ['IT']*30 + ['HR']*30,
'Salary': [50000, 52000, 55000, 48000, 53000]*6 +
[70000, 72000, 75000, 68000, 73000]*6 +
[45000, 47000, 48000, 44000, 46000]*6
})
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
# Box plot
sns.boxplot(data=df, x='Department', y='Salary', ax=axes[0])
axes[0].set_title('Box Plot')
# Violin plot
sns.violinplot(data=df, x='Department', y='Salary', ax=axes[1])
axes[1].set_title('Violin Plot')
# Strip plot
sns.stripplot(data=df, x='Department', y='Salary', ax=axes[2])
axes[2].set_title('Strip Plot')
plt.tight_layout()
plt.show()Key Points
- kde=True adds smooth distribution line
- regplot shows trend line automatically
- pairplot shows all variable relationships
- heatmap visualizes correlations
- violinplot shows distribution shape
- All work directly with DataFrames
What's Next?
Learn to customize Seaborn with themes and styles.