45 · Seaborn: statistical visualization on top of matplotlib
Seaborn sits on top of matplotlib and brings a DataFrame-native API, sensible statistical defaults, and plot types tailored for data analysis — scatter with class coloring, box plots per category, and the correlation heatmap that every EDA notebook starts with.
Seaborn's key design decision is **tabular input**: you pass a DataFrame plus column name strings (`x='feat1'`, `y='feat2'`, `hue='label'`) and seaborn handles axis setup, color palettes, statistical aggregations, and legend generation. For matplotlib you build each of these yourself; seaborn is the shortcut that gets you to an informative figure in two lines instead of twenty.
Without this:
Without seaborn, every EDA scatter with class coloring requires ~15 lines of matplotlib: group by class, loop over groups, scatter each group with a different color, build a legend. Seaborn does that in one line. The heatmap of feature correlations — a standard first step in any EDA — is similarly verbose to build from scratch.
Seaborn is a statistical visualization library built on top of matplotlib. It adds two things matplotlib doesn't have out of the box:
- Tabular (DataFrame) input — instead of passing raw arrays, you pass a DataFrame and the names of the columns you want on each axis. Seaborn handles splitting, aggregating, and coloring.
- Statistical defaults — histograms with KDE curves, scatter plots with confidence intervals on regression lines, box plots with outlier detection — all automatic.
The universal import alias is import seaborn as sns (named after the TV character Sam Seaborn).
Key plot types you'll use in every EDA:
| Function | Use case |
|---|---|
| sns.histplot(data, x=col) | Distribution of one numeric variable |
| sns.scatterplot(data, x, y, hue=class) | Two variables colored by class — shows separability |
| sns.boxplot(data, x=category, y=value) | Value distribution per category — outlier detection |
| sns.heatmap(corr_matrix, annot=True) | Feature-correlation matrix — multicollinearity check |
| sns.pairplot(df) | All pairs of numeric columns — expensive, small datasets only |
Seaborn returns a matplotlib Axes (or Figure for grid plots like pairplot), so you can mix seaborn and matplotlib calls freely on the same axes.
Python (in browser)
`sns.scatterplot(data=df, x='feat1', y='feat2', hue='label')` replaces ~15 lines of matplotlib loop-scatter code. The `hue=` parameter maps a categorical column to colors and auto-generates the legend. `palette=` lets you control exact colors per category.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
`df.corr(numeric_only=True)` returns a square correlation matrix. `sns.heatmap(corr, annot=True, cmap='coolwarm')` colors cells from blue (negative correlation) through white (zero) to red (positive). `vmin=-1, vmax=1` anchors the colormap so the zero-correlation color is always white — without this, the scale floats with the data range.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
`sns.pairplot` returns a `PairGrid` object whose `.figure` attribute is the underlying matplotlib `Figure`. The cost is quadratic in the number of columns — use it only on DataFrames with ≤10 numeric columns.
In `sns.scatterplot(data=df, x='feat1', y='feat2', hue='label')`, what does the `hue='label'` argument do?
- Seaborn's tabular API (`data=df, x='col', y='col', hue='class'`) collapses ~15 lines of matplotlib groupby-loop code into one line.
- `sns.heatmap(df.corr(numeric_only=True), annot=True, cmap='coolwarm')` is the standard first-look at feature multicollinearity.
- Seaborn figures are matplotlib under the hood — you can call `ax.set_title(...)` and `fig.savefig(...)` on the objects seaborn returns.
- Never run `sns.pairplot` on a DataFrame with more than ~8 numeric columns — it generates N² subplots and will hang the kernel.
The EDA pipeline for every ML project runs in this order: `sns.scatterplot` with `hue=class` to see separability; `sns.heatmap(corr)` to spot multicollinearity (correlated features → drop one); `sns.boxplot` per class to find outliers and distribution differences. These three plots answer the three questions you must answer before fitting any model.
If you remove it: Without seaborn (or equivalent), EDA visualization takes 10× longer to write. More importantly, you'd likely skip it and go straight to model fitting — which is how you end up debugging a model that fails silently because two of your features are 0.98 correlated and act as duplicates.