Seaborn for Statistical Plots
Seaborn sits on top of matplotlib and makes attractive statistical charts easy.
Seaborn is a higher-level library built on matplotlib. It understands DataFrames and produces polished statistical charts with one call.
Handy plots
sns.histplotandsns.kdeplotfor distributions.sns.scatterplotwith automatic colour by category.sns.heatmapfor correlation matrices — great for feature selection.
Example
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({
'height': [160, 170, 180, 175, 165],
'weight': [60, 72, 85, 78, 66],
'sex': ['F', 'M', 'M', 'M', 'F'],
})
sns.scatterplot(data=df, x='height', y='weight', hue='sex')
plt.show()
# Correlation heatmap of numeric columns
sns.heatmap(df[['height', 'weight']].corr(), annot=True)
plt.show()When to use it
- A data scientist uses seaborn's heatmap to visualise the feature correlation matrix, quickly spotting highly correlated predictors to remove.
- An analyst uses sns.boxplot grouped by category to compare distributions and identify outliers across product lines in a single chart.
- An ML researcher uses sns.pairplot on a feature DataFrame to explore all pairwise relationships and class separability in one call.
More examples
Correlation heatmap
Computes the pairwise Pearson correlation and visualises it as a colour-coded heatmap with annotated coefficients.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('features.csv').select_dtypes('number')
corr = df.corr()
sns.heatmap(corr, annot=True, fmt='.2f', cmap='coolwarm')
plt.title('Feature Correlation Matrix')
plt.show()Boxplot: distribution by category
Draws a grouped boxplot from the built-in tips dataset showing the median, quartiles, and outliers of the bill for each day of the week.
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
sns.boxplot(data=tips, x='day', y='total_bill', palette='Set2')
plt.title('Bill distribution by day')
plt.show()Pairplot for feature exploration
Generates a grid of scatter plots and KDE diagonals for every feature pair in the Iris dataset, coloured by species to assess class separability at a glance.
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris(as_frame=True)
df = iris.frame
df['target_name'] = iris.target_names[iris.target]
sns.pairplot(df, hue='target_name', diag_kind='kde')
plt.suptitle('Iris Pairplot', y=1.02)
plt.show()
Discussion