Statistical Plotting with Seaborn
Use Seaborn for statistical relationships, facets, and attractive default aesthetics.
In plain words
Seaborn turns a table into good-looking statistical charts by letting you say which column goes where, such as colour by group or one panel per category.
Think of it like…
A smart camera preset: you say what you're photographing (relationship, distribution or comparison) and which attributes matter, and it sets exposure, framing and labels for you, while Matplotlib remains the manual mode underneath.
Where you’ve seen it
On a restaurant-tips DataFrame of 244 rows (total_bill, tip, day, time, smoker), one call, sns.relplot(data=tips, x="total_bill", y="tip", hue="smoker", col="time"), draws two panels (Lunch and Dinner) with points coloured by smoker status, revealing that tip grows roughly linearly with the bill and that dinner bills spread wider. sns.catplot(data=tips, x="day", y="tip", kind="box") compares tip distributions by weekday, and sns.barplot(data=tips, x="day", y="tip") shows the mean tip per day with a 95% bootstrap interval (by default 1,000 resamples), reminding the analyst that bar height here is a mean, not a count.
Words you’ll hear
- Tidy / long-form data
- One row per observation, one column per variable, the shape Seaborn expects.
- Axes-level vs figure-level
- Functions that draw on one Axes versus functions that build a FacetGrid of Axes.
- Semantic mapping
- Assigning columns to hue, size, style, row or col.
- Estimator and error bar
- The statistic (mean, median) and uncertainty (bootstrap CI, SD) Seaborn computes for you.
Visual diagram
Theory & deep dive
Data shape, function levels and semantics
Seaborn expects long-form data: if you have columns q1, q2, q3, q4, reshape them with df.melt(id_vars="region", var_name="quarter", value_name="sales") so "quarter" can map to x or hue. Axes-level functions accept an ax= argument and return an Axes, so they compose with Matplotlib subplots. Figure-level functions (relplot, displot, catplot, plus lmplot, pairplot, jointplot) create their own figure, accept col, row, col_wrap, height and aspect, and return a FacetGrid; don't pass them an ax. Customise with g.set_axis_labels, g.set_titles and g.figure.suptitle.
The semantic mappings are Seaborn's strength: hue colours by a category or number, style varies marker or dash, size scales points, col/row facet. Prefer mapping the most important comparison to position, then hue, then facets; avoid combining hue, style and size all on the same variable. Choose palettes to match data type (qualitative for categories, sequential or diverging for numeric), and set the look once with sns.set_theme(style="whitegrid", context="talk", palette="colorblind").
long = sales.melt(id_vars="region", var_name="quarter", value_name="sales") then sns.catplot(data=long, x="quarter", y="sales", hue="region", kind="bar"). Without the melt, you'd have to plot each quarter column manually.
Built-in statistics and what to watch for
Several Seaborn functions aggregate silently. barplot and pointplot default to the mean with a 95% bootstrap confidence interval (errorbar=("ci", 95), n_boot=1000); lineplot aggregates repeated x values the same way, shading the interval. Use estimator="median" or errorbar="sd" when appropriate, countplot for counts, and show raw data (stripplot, swarmplot or kind="box" with points) when sample sizes are small so groups with few observations aren't hidden behind a tidy bar. Distribution functions choose bins and KDE bandwidth automatically; override bins, binwidth or bw_adjust and compare, since the picture can change.
Pitfalls: forgetting that bar height is a mean (a bar of 3.0 might hide a bimodal distribution); plotting huge data with swarmplot (slow and overplotted: sample or use boxenplot); letting category order default to appearance order (set order= or an ordered categorical dtype); using hue for a numeric variable with many unique values; and mixing figure-level output into Matplotlib subplots. Seaborn also does not replace analysis: intervals assume independent observations, so for clustered data (repeated measures) aggregate first or use appropriate models.
sns.barplot(data=tips, x="day", y="tip") shows the mean tip per day with a bootstrap CI. Adding sns.stripplot(data=tips, x="day", y="tip", color="k", alpha=0.4) on the same Axes reveals that Sunday has more observations and a longer upper tail than the mean suggests.
How it works, step by step
Step 1 of 41. Get tidy data
Ensure one row per observation; melt wide tables and set ordered categoricals where order matters.
- Check
- dtypes and categories
When to use it & trade-offs
What it is
Seaborn is a statistical visualisation library built on Matplotlib that works directly with tidy (long-form) DataFrames: you map columns to visual properties (x, y, hue, size, style, col, row) and it handles aggregation, confidence intervals, legends and tasteful default themes. Its functions come in two levels: axes-level (scatterplot, lineplot, histplot, boxplot, barplot) that draw onto one Matplotlib Axes, and figure-level (relplot, displot, catplot) that create a faceted grid of panels for you. The three families are relational (relationships between numeric variables), distributional (shape of a variable) and categorical (comparisons across groups).
Why it matters: Exploratory analysis is mostly asking "how does Y differ by group, and does it depend on a third variable?". Seaborn answers that in one line of code, with sensible statistics (means with bootstrapped intervals, kernel densities) built in, so analysts spend time interpreting rather than wiring subplots by hand. Knowing its data model also prevents the classic traps: wide versus long data, silent aggregation and mixing figure-level functions into existing subplots.
When to reach for it
- Exploratory analysis of group differences
- Faceted comparisons with one line of code
- Quick, readable distribution and relationship plots
When to avoid or be careful
- Bar charts of means when group sizes are tiny and unshown
- Passing ax= to figure-level functions
- Plotting wide DataFrames without reshaping
Seaborn vs adjacent options
Strengths: Concise, built-in aggregation and facets, good defaults.
Watch out: Less control than raw Matplotlib; silent aggregation.
Strengths: Complete control.
Watch out: More code for common statistical plots.
Strengths: Hover and zoom for free.
Watch out: Heavier output; different styling model.
Interview signals & numbers to know
When this topic comes up
- “Compare distributions across categories”
- “Facet by segment”
- relplot vs scatterplot
- Bootstrap confidence intervals in a bar chart
- Wide versus long format
Benchmarks & rules of thumb
- 95%
- default confidence level for barplot and lineplot intervals
- 1,000
- default bootstrap resamples (n_boot)
- 244
- rows in the classic tips dataset
Common mistakes & follow-ups
Common interview mistakes
- Assuming a barplot bar is a count rather than a mean.
- Leaving category order to appearance order.
- Using swarmplot on tens of thousands of points.
Likely interviewer follow-ups
- What is the difference between relplot and scatterplot?
- Why does Seaborn want long-form data?
- How would you show both the mean and the raw data for groups?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Statistical Plotting with Seaborn with data science interview Q&A and hands-on practice.