Visualization
Intermediate 14 min read Visualization

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.

seabornrelplotdisplotboxplotheatmapstatistical inference
Prerequisites: Matplotlib Basics
Before we start

What you will learn

  • Plot relationships and distributions by groups
  • Use box/violin for comparisons with uncertainty
  • Generate correlation heatmaps
Introduction

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.

Motivation

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.
Think about it

Mental model

Imagine

A structured cooking assistant that does the mise en place

Think of

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.

Mechanics

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.

Data + columns
Relationship
Distribution
Matrix
Facets
Hands-on

Examples

Faceted statistical plot

Faceted statistical plot
import seaborn as sns, matplotlib.pyplot as plt, numpy as np, pandas as pd
df = 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.

Real world

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.
Guidelines

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.
Watch out

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.
When things break

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.
Interviews

Interview checkpoint

Practice

Hands-on lab

Statistical Plots in Minutes
25 min · intermediate
In progress
Objectives
  • Build grouped distributions vs a log scale
  • Facet small multiples
  • Plot a correlation heatmap

Statistical Plots in Minutes

25 min · intermediate

0/5 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Which Seaborn call shows the correlation matrix of numeric columns?

2

A regplot's shaded band is the confidence interval of the fitted line, not the data spread.

Go deeper

Official documentation