LESSONS · 10 · 07 / 10
Seaborn: Statistical Data Visualization
Elevate your visualizations with seaborn. Create beautiful statistical graphics with minimal code: distributions, relationships, and categorical plots.
TIPLearning Objectives: After this lesson, you'll create beautiful statistical graphics with minimal code using seaborn—distributions, relationships, categorical comparisons, and styled presentations.
TIPHow to read this lesson. Seaborn isn't bundled in this in-browser Python sandbox, but matplotlib is — and seaborn is a thin, opinionated layer over matplotlib. So every runnable cell below renders a real chart with matplotlib, and each block names the one-line seaborn equivalent (marked
≡ sns.…). You learn what the chart is, and the exact seaborn call you'd reach for in your own environment.
Why Seaborn?
Before writing any code, get a feel for what a statistical chart actually communicates. The plotter below shows a frequency distribution—the same kind of shape sns.histplot() produces.
Try it: Switch the chart type and tweak the values, then watch the shape redraw. Notice how a tall center with tapering sides reads as a "normal" distribution—the exact pattern the histogram code below generates.
Seaborn is built on matplotlib but provides a high-level interface for creating attractive statistical graphics. What takes ten lines in matplotlib often takes one in seaborn.
Plotting one scatter per category in raw matplotlib is a loop:
import matplotlib.pyplot as plt fig, ax = plt.subplots() for group in data['category'].unique(): subset = data[data['category'] == group] ax.scatter(subset['x'], subset['y'], label=group) ax.legend() ax.set_xlabel('X') ax.set_ylabel('Y') plt.show()
Seaborn collapses the whole loop — grouping, coloring, and the legend — into a single call:
sns.scatterplot(data=data, x='x', y='y', hue='category')
That economy is the whole point. Seaborn gives you beautiful defaults, built-in statistical aggregation (means, confidence intervals, KDEs), automatic legends and labels, native pandas integration, and a deep catalog of specialized statistical plots.
Setting Up Seaborn
Seaborn ships with sensible defaults you set once per session:
- Styles (
sns.set_theme(style=...)):whitegridanddarkgridadd gridlines;white,dark, andtickskeep things minimal. - Palettes (
sns.set_palette(...)):deepis the default,muted/pastelare softer,husl/brightare colorful, andcolorblindis accessibility-safe.
In a real project you import seaborn and pick a theme and palette once, up front:
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd sns.set_theme() # Apply seaborn's default look # Themes — overall background + grid sns.set_theme(style='whitegrid') # Clean, with a grid (great for stats) sns.set_theme(style='darkgrid') # Dark background with grid sns.set_theme(style='white') # Minimal, no grid sns.set_theme(style='ticks') # Axis ticks, no grid # Palettes — the sequence of colors used for categories sns.set_palette('deep') # Default sns.set_palette('muted') # Softer sns.set_palette('colorblind') # Accessibility-safe
The single most useful habit: reach for colorblind or a perceptually-uniform palette like viridis so your charts stay readable for everyone.
Distribution Plots
Understanding data distributions is fundamental to data analysis.
Histograms and KDE
(The frequency-distribution plotter at the top of this lesson previews the shape this code produces.)
Box Plots and Violin Plots
Relationship Plots
Explore relationships between variables.
Scatter Plots with Regression
Pair Plots
Visualize relationships between all pairs of variables:
Heatmaps
Visualize matrices and correlations:
Categorical Plots
Compare categories effectively.
Bar Plots with Error Bars
Count Plots
Strip and Swarm Plots
Show individual data points:
Combining Plots
FacetGrid: Multiple Subplots
Create a grid of plots based on data subsets:
Complete Example: EDA Dashboard
Key Takeaways
✅ Distribution plots: histplot(), kdeplot(), boxplot(), violinplot()
✅ Relationship plots: scatterplot(), regplot(), pairplot(), heatmap()
✅ Categorical plots: barplot(), countplot(), stripplot(), swarmplot()
✅ FacetGrid: Create grids of plots by data subsets
✅ Styling: set_theme(), color palettes, context settings
✅ Integration: Works seamlessly with pandas DataFrames
Connections: Seaborn in Practice
🔗 Connection to Statistics
| Statistical Concept | Seaborn Plot |
|---|---|
| Distribution | histplot(), kdeplot() |
| Central tendency | barplot() (shows mean) |
| Spread | boxplot(), violinplot() |
| Correlation | heatmap(), pairplot() |
| Regression | regplot(), lmplot() |
🔗 When to Use Which Plot
| Question | Plot Type |
|---|---|
| What's the distribution? | histogram, KDE, box |
| Compare categories? | bar, count, box |
| Relationship between 2 vars? | scatter, regression |
| All pairwise relationships? | pairplot |
| Matrix of values? | heatmap |
Practice Exercises
Exercise 1: Complete Visualization
Next Steps
Now you're ready for Exploratory Data Analysis (EDA)—a systematic approach to understanding data by combining all the techniques you've learned.
Ready to explore real datasets? EDA is next!
Further Reading
Visual Galleries
- Seaborn Example Gallery — every chart type with thumbnail + code. Bookmark.
- From Data to Viz — flowchart-driven guide: pick your data shape, get the right chart and the seaborn snippet.
- Python Graph Gallery — Seaborn — 200+ examples organized by chart type.
Official Docs
- Seaborn — Tutorial — official walkthrough.
- Seaborn — Figure-level vs Axes-level Functions — the most important conceptual distinction in modern seaborn.
- Seaborn — Color Palettes — colorblind-safe and perceptually-uniform palettes (
viridis,cividis,colorblind).
Tutorials
- Real Python — Python Plotting With Seaborn — the canonical primer.
- seaborn.objects — declarative interface — the new pipe-friendly API.
Modern Declarative Alternatives
- Altair — Vega-Lite-based grammar of graphics. Most principled "declarative viz" library in Python.
- plotnine —
ggplot2ported to Python. If you came from R, start here.
Books
- Book: Storytelling with Data — Cole Nussbaumer Knaflic. The why of visualization.
- Book: Fundamentals of Data Visualization — Claus Wilke (free online). Library-agnostic; required reading for anyone making serious charts.