Seaborn Statistical Visualization
Overview
Seaborn is a Python visualization library for creating publication-quality statistical graphics. Use this skill for dataset-oriented plotting, multivariate analysis, automatic statistical estimation, and complex multi-panel figures with minimal code.
When to Use This Skill
Use seaborn for quick, attractive statistical graphics straight from a pandas DataFrame: distributions, relationships, categorical comparisons, correlation heatmaps, and faceted small multiples. Route elsewhere when the need differs:
- Interactive charts (hover, zoom, HTML dashboards) →
alterlab-plotly
- Exact journal/manuscript styling (column widths, point fonts, CMYK, vector export) →
alterlab-scientific-viz
- Low-level custom plotting →
alterlab-matplotlib (seaborn integrates with it for fine-tuning)
Design Philosophy
- Dataset-oriented — work directly with DataFrames and named variables, not abstract coordinates.
- Semantic mapping — automatically translate data values into visual properties (color, size, style).
- Statistical awareness — built-in aggregation, error estimation, and confidence intervals.
- Aesthetic defaults — publication-ready themes and palettes out of the box.
- Matplotlib integration — full compatibility with matplotlib customization when needed.
Quick Start
Examples target seaborn ≥ 0.13 (verified on 0.13.2). Two API points that bite on this version: pass palette= only together with hue= (palette-without-hue is deprecated, removed in 0.14), and style error bars via err_kws={...} rather than the removed-in-0.15 errcolor/errwidth/scale/join keywords.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
df = sns.load_dataset('tips')
sns.scatterplot(data=df, x='total_bill', y='tip', hue='day')
plt.show()
Core Workflow
- Shape the data as long-form ("tidy") — one column per variable, one row per observation. This works with every seaborn function. Reshape wide data with
df.melt(...). See references/data_palettes_theming.md.
- Pick the plot category for your variable types (see routing below).
- Encode extra dimensions with
hue, size, style semantic mappings.
- Choose axes-level vs figure-level: axes-level (
scatterplot, boxplot, heatmap, …) plug into custom matplotlib layouts via ax=; figure-level (relplot, displot, catplot, lmplot, …) own the whole figure and facet via col/row.
- Theme and save with
set_theme/set_context and savefig(dpi=300, bbox_inches='tight') (PDF for vector).
Plot Category Routing
Choose the category, then see references/plotting_functions.md for parameters and code for each.
| Goal |
Category |
Key functions |
| How variables relate |
Relational |
scatterplot, lineplot, relplot |
| Spread / shape / density |
Distribution |
histplot, kdeplot, ecdfplot, displot, jointplot, pairplot |
| Compare across categories |
Categorical |
stripplot, swarmplot, boxplot, violinplot, barplot, pointplot, countplot, catplot |
| Linear relationships / residuals |
Regression |
regplot, lmplot, residplot |
| Matrices / correlations |
Matrix |
heatmap, clustermap |
| Custom multi-panel grids |
Grids |
FacetGrid, PairGrid, JointGrid |
The modern declarative seaborn.objects interface (ggplot2-like, composable) is best for complex layered or programmatic plots — see references/objects_interface.md.
Color and Theming (essentials)
- Qualitative palettes for categories (
"colorblind", "deep", "muted"); sequential for ordered data ("rocket", "viridis"); diverging for centered data ("vlag", "coolwarm", with center=0).
set_theme(style=..., context=..., palette=...); styles whitegrid/ticks/…; contexts paper→talk→poster scale element sizes.
Full palette and theming reference: references/data_palettes_theming.md.
Best Practices (essentials)
- Plot from named DataFrame columns (preserves axis labels); use figure-level functions for faceting; encode extra dimensions with
hue/size/style.
- Know what each function estimates:
lineplot/barplot auto-compute mean + CI — override with errorbar= and estimator=.
- Combine with matplotlib (
ax.set(...), axhline, tight_layout) for fine-tuning; save at dpi=300, and PDF for publications.
Full best-practices, common patterns, and troubleshooting (legend placement, overlapping labels, figure sizing, palette distinctness, KDE bandwidth): references/best_practices_and_troubleshooting.md.
Reference Index
references/plotting_functions.md — every plot category with parameters and code (relational, distribution, categorical, regression, matrix, multi-plot grids, figure-vs-axes-level).
references/data_palettes_theming.md — long/wide data structure, color palettes (qualitative/sequential/diverging/custom), and theming (set_theme, styles, contexts).
references/best_practices_and_troubleshooting.md — best practices, common patterns (EDA, publication figures, multi-panel, time series), and troubleshooting.
references/function_reference.md — comprehensive function signatures, parameters, and examples.
references/objects_interface.md — detailed guide to the modern seaborn.objects API.
references/examples.md — scenario-based worked examples and code patterns.
1---2name: alterlab-seaborn3description: Builds statistical plots with the seaborn Python library and pandas DataFrame integration, on attractive matplotlib-based defaults. Use for quick exploration of distributions, relationships, and categorical comparisons — box plots, violin plots, swarm/strip plots, KDE/histograms, pair plots, joint plots, regression plots, correlation heatmaps, and faceted small multiples (relplot/displot/catplot/lmplot). For interactive/hover/zoom charts defer to alterlab-plotly; for exact journal/manuscript styling (column widths, point fonts, CMYK, vector export) defer to alterlab-scientific-viz; for low-level custom matplotlib figures defer to alterlab-matplotlib (seaborn integrates with it for fine-tuning). Part of the AlterLab Academic Skills suite.4license: MIT5---67# Seaborn Statistical Visualization89## Overview1011Seaborn is a Python visualization library for creating publication-quality statistical graphics. Use this skill for dataset-oriented plotting, multivariate analysis, automatic statistical estimation, and complex multi-panel figures with minimal code.1213## When to Use This Skill1415Use seaborn for quick, attractive statistical graphics straight from a pandas DataFrame: distributions, relationships, categorical comparisons, correlation heatmaps, and faceted small multiples. Route elsewhere when the need differs:1617- **Interactive charts** (hover, zoom, HTML dashboards) → `alterlab-plotly`18- **Exact journal/manuscript styling** (column widths, point fonts, CMYK, vector export) → `alterlab-scientific-viz`19- **Low-level custom plotting** → `alterlab-matplotlib` (seaborn integrates with it for fine-tuning)2021## Design Philosophy22231. **Dataset-oriented** — work directly with DataFrames and named variables, not abstract coordinates.242. **Semantic mapping** — automatically translate data values into visual properties (color, size, style).253. **Statistical awareness** — built-in aggregation, error estimation, and confidence intervals.264. **Aesthetic defaults** — publication-ready themes and palettes out of the box.275. **Matplotlib integration** — full compatibility with matplotlib customization when needed.2829## Quick Start3031Examples target **seaborn ≥ 0.13** (verified on 0.13.2). Two API points that bite on this version: pass `palette=` only together with `hue=` (palette-without-hue is deprecated, removed in 0.14), and style error bars via `err_kws={...}` rather than the removed-in-0.15 `errcolor`/`errwidth`/`scale`/`join` keywords.3233```python34import seaborn as sns35import matplotlib.pyplot as plt36import pandas as pd3738df = sns.load_dataset('tips')39sns.scatterplot(data=df, x='total_bill', y='tip', hue='day')40plt.show()41```4243## Core Workflow44451. **Shape the data** as long-form ("tidy") — one column per variable, one row per observation. This works with every seaborn function. Reshape wide data with `df.melt(...)`. See `references/data_palettes_theming.md`.462. **Pick the plot category** for your variable types (see routing below).473. **Encode extra dimensions** with `hue`, `size`, `style` semantic mappings.484. **Choose axes-level vs figure-level**: axes-level (`scatterplot`, `boxplot`, `heatmap`, …) plug into custom matplotlib layouts via `ax=`; figure-level (`relplot`, `displot`, `catplot`, `lmplot`, …) own the whole figure and facet via `col`/`row`.495. **Theme and save** with `set_theme`/`set_context` and `savefig(dpi=300, bbox_inches='tight')` (PDF for vector).5051## Plot Category Routing5253Choose the category, then see `references/plotting_functions.md` for parameters and code for each.5455| Goal | Category | Key functions |56|------|----------|---------------|57| How variables relate | Relational | `scatterplot`, `lineplot`, `relplot` |58| Spread / shape / density | Distribution | `histplot`, `kdeplot`, `ecdfplot`, `displot`, `jointplot`, `pairplot` |59| Compare across categories | Categorical | `stripplot`, `swarmplot`, `boxplot`, `violinplot`, `barplot`, `pointplot`, `countplot`, `catplot` |60| Linear relationships / residuals | Regression | `regplot`, `lmplot`, `residplot` |61| Matrices / correlations | Matrix | `heatmap`, `clustermap` |62| Custom multi-panel grids | Grids | `FacetGrid`, `PairGrid`, `JointGrid` |6364The modern declarative `seaborn.objects` interface (ggplot2-like, composable) is best for complex layered or programmatic plots — see `references/objects_interface.md`.6566## Color and Theming (essentials)6768- **Qualitative** palettes for categories (`"colorblind"`, `"deep"`, `"muted"`); **sequential** for ordered data (`"rocket"`, `"viridis"`); **diverging** for centered data (`"vlag"`, `"coolwarm"`, with `center=0`).69- `set_theme(style=..., context=..., palette=...)`; styles `whitegrid`/`ticks`/…; contexts `paper`→`talk`→`poster` scale element sizes.7071Full palette and theming reference: `references/data_palettes_theming.md`.7273## Best Practices (essentials)7475- Plot from named DataFrame columns (preserves axis labels); use figure-level functions for faceting; encode extra dimensions with `hue`/`size`/`style`.76- Know what each function estimates: `lineplot`/`barplot` auto-compute mean + CI — override with `errorbar=` and `estimator=`.77- Combine with matplotlib (`ax.set(...)`, `axhline`, `tight_layout`) for fine-tuning; save at `dpi=300`, and PDF for publications.7879Full best-practices, common patterns, and troubleshooting (legend placement, overlapping labels, figure sizing, palette distinctness, KDE bandwidth): `references/best_practices_and_troubleshooting.md`.8081## Reference Index8283- **`references/plotting_functions.md`** — every plot category with parameters and code (relational, distribution, categorical, regression, matrix, multi-plot grids, figure-vs-axes-level).84- **`references/data_palettes_theming.md`** — long/wide data structure, color palettes (qualitative/sequential/diverging/custom), and theming (`set_theme`, styles, contexts).85- **`references/best_practices_and_troubleshooting.md`** — best practices, common patterns (EDA, publication figures, multi-panel, time series), and troubleshooting.86- **`references/function_reference.md`** — comprehensive function signatures, parameters, and examples.87- **`references/objects_interface.md`** — detailed guide to the modern `seaborn.objects` API.88- **`references/examples.md`** — scenario-based worked examples and code patterns.