Seaborn Statistical Plots
Statistics-aware plotting on top of matplotlib
Seaborn turns dataframes into statistical graphics — relplots, displots, boxplots, heatmaps, regression fits — adding uncertainty and grouping with one call.
What you will learn
- Plot relationships and distributions by groups
- Use box/violin for comparisons with uncertainty
- Generate correlation heatmaps
What is this?
Seaborn is a statistical visualization layer over matplotlib. It maps dataframe columns to visual channels with group-by semantics built in: sns.relplot for scatter/line, sns.displot for histograms/KDE, sns.boxplot/violinplot for distributions, sns.heatmap for matrices, sns.regplot for fits with CIs.
Why do you need it?
- Checked-in exploratory code reads 'dataframe → plot with groups' in one line.
- Uncertainty (bootstrap error bars, KDE density) comes included.
- Faceting by a category creates small multiples without loops.
- Ship charts that already look 'report-styled' instead of raw matplotlib defaults.
Mental model
A structured cooking assistant that does the mise en place
Seaborn over matplotlib
Tell it 'these are the ingredients and the grouping' and it produces the dish directly — the grouping, the uncertainty band, the faceting handled for you — saving you from hand-carving every detail in matplotlib.
How it works
- sns.set_theme() selects a coherent style; plots accept data=df and column names.
- sns.boxplot(data=df, x='region', y='rev') compares distributions across categories.
- sns.heatmap(df.corr(), annot=True, cmap='coolwarm') renders the correlation matrix.
- sns.relplot(..., hue='tier', style=...) adds grouping; col= facets columns.
- sns.regplot adds a fitted line with a translucent confidence band.
Mapping a dataframe to statistical views.
Examples
Faceted statistical plot
import seaborn as sns, matplotlib.pyplot as plt, numpy as np, pandas as pddf = pd.DataFrame({ "region": ["eu","eu","us","us"] * 50, "tier": (["pro","free"] * 50) * 2, "rev": np.random.default_rng(0).lognormal(2, .5, 200),})g = sns.boxplot(data=df, x="region", y="rev", hue="tier")g.set_yscale("log")plt.tight_layout(); plt.savefig("bx.png", dpi=120)One call produces grouped box plots with the tier contrast and the log scale handling skew. Seaborn does the aggregation, styling and legend — the matplotlib-level loop would be 15 lines longer.
In production
- An A/B analysis uses sns.pointplot with error bars to show treatment differences across cohorts.
- A model-debug run visualizes residual vs fitted with sns.residplot to spot heteroscedasticity.
- A churn deck uses sns.FacetGrid box plots grouped by plan and region.
Best practices
- Always specify data= and column names; avoid pandas-frame positional hacks.
- Use hue for one grouping max; facet (col=) for the second.
- Log-scale skewed axes before judging group box relationships.
Common mistakes
- Forgetting data=, passing raw arrays with mismatched shapes.
- Contrasting means via defaults while missing the box median whiskers.
- Plotting a heatmap of raw data instead of the correlation/pivot.
Debugging tips
- If hue groups don't show, check the column is categorical dtype or pass order=.
- A white box in a heatmap usually means NaN in corr — drop or fill the pair.
- Log-scale plot vs linear — many box 'differences' vanish on log.
Interview checkpoint
Hands-on lab
- Build grouped distributions vs a log scale
- Facet small multiples
- Plot a correlation heatmap
Statistical Plots in Minutes
25 min · intermediate
Quick quiz
Which Seaborn call shows the correlation matrix of numeric columns?
A regplot's shaded band is the confidence interval of the fitted line, not the data spread.