python3 min read

Seaborn Tutorial: Learn Statistical Visualization from Scratch (2026)

Seaborn Tutorial: Learn Statistical Visualization from Scratch (2026)

Published:  |  Category: Python  |  Reading time: ~15 min
Seaborn Tutorial: Learn Statistical Visualization from Scratch (2026)

After years of tweaking Matplotlib defaults by hand, Seaborn felt like cheating. Built on top of Matplotlib, it provides high-level functions for statistical plots that would take dozens of lines of raw Matplotlib code. A single call to sns.boxplot or sns.heatmap produces a publication-ready chart with sensible defaults, automatic legends, and integrated pandas support.

This tutorial covers Seaborn's core plotting functions: categorical and relational plots, distribution visualization, heatmaps for correlation, and customizing themes. Every example uses a real dataset (tips, penguins, or flights) so you can run the code yourself and see exactly what each parameter controls.

Seaborn Themes and Context

Seaborn changes Matplotlib's default style. set_theme() applies a clean style with grid lines, readable fonts, and muted colors. set_context() adjusts scaling for different media — 'paper', 'notebook', 'talk', 'poster'. I always call sns.set_theme(style='whitegrid') at the start of a notebook for consistent visuals.

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(style='whitegrid', palette='viridis', font_scale=1.2)

df = sns.load_dataset('penguins')
print(df.head())

Relational Plots: Scatter and Line

relplot() is the unified function for relational plots. kind='scatter' shows the relationship between two numeric variables, with options for color (hue), size, and style mapping to other columns. kind='line' draws a line through the data with shaded confidence intervals.

sns.relplot(
    data=df,
    x='bill_length_mm', y='bill_depth_mm',
    hue='species', size='body_mass_g',
    alpha=0.7, palette='deep'
)
plt.title('Bill dimensions by species')
plt.show()

flights = sns.load_dataset('flights')
sns.relplot(
    data=flights,
    x='year', y='passengers',
    hue='month', kind='line',
    col='month', col_wrap=4
)

Categorical Plots: Box, Violin, and Bar

For comparing distributions across categories, catplot() is the workhorse. kind='box' gives the classic box plot with median, quartiles, and outliers. kind='violin' combines box plot with kernel density, showing the full distribution shape. kind='bar' displays the mean with error bars.

sns.catplot(data=df, x='species', y='body_mass_g', kind='box', height=5)
plt.title('Body mass distribution by species')
plt.show()

sns.catplot(
    data=df.dropna(),
    x='species', y='bill_length_mm',
    hue='sex', kind='violin', split=True,
    height=5, aspect=1.2
)
plt.show()

sns.catplot(data=df, x='island', y='flipper_length_mm', kind='bar', height=4)

Distribution Plots: Histogram, KDE, and ECDF

histplot shows binned counts with optional KDE overlay. kdeplot estimates the probability density function directly. ecdfplot shows the cumulative distribution, which is useful for comparing distributions without binning artifacts.

sns.histplot(data=df, x='body_mass_g', hue='species', kde=True, alpha=0.5)
plt.title('Body mass with KDE overlay')
plt.show()

sns.kdeplot(data=df, x='bill_length_mm', hue='species', fill=True, alpha=0.3)
plt.show()

sns.ecdfplot(data=df, x='flipper_length_mm', hue='species')
plt.title('Cumulative distribution of flipper length')
plt.show()

Heatmaps for Correlation Matrices

A heatmap visualizes a matrix of values as a color-coded grid. The most common use is showing the correlation matrix of numeric features. sns.heatmap accepts a 2D array, adds annotations with annot=True, and uses a diverging colormap (coolwarm) to show positive and negative correlations.

import numpy as np

corr = df.select_dtypes(include=[np.number]).corr()

mask = np.triu(np.ones_like(corr, dtype=bool))

plt.figure(figsize=(8, 6))
sns.heatmap(
    corr,
    mask=mask,
    annot=True,
    fmt='.2f',
    cmap='coolwarm',
    vmin=-1, vmax=1,
    center=0,
    square=True,
    linewidths=0.5
)
plt.title('Feature Correlation Matrix')
plt.tight_layout()
plt.show()

Pair Plots and Customizing with Matplotlib

pairplot() creates a matrix of scatter plots for every numeric variable pair, with histograms on the diagonal. It's invaluable for EDA — I spot clusters, outliers, and relationships in seconds. Since Seaborn builds on Matplotlib, you can combine them for further customization.

pp = sns.pairplot(
    df.dropna(),
    hue='species',
    diag_kind='kde',
    palette='husl',
    corner=True
)
pp.fig.suptitle('Penguin features pair plot', y=1.02)
plt.show()

fig, ax = plt.subplots()
sns.boxplot(data=df, x='species', y='body_mass_g', ax=ax)
ax.set_title('Customized Box Plot')
ax.set_ylabel('Body Mass (g)')
ax.grid(axis='y', alpha=0.3)
plt.show()

Frequently Asked Questions

Do I need to use Matplotlib with Seaborn?

Seaborn uses Matplotlib under the hood. You can use plt for saving figures (plt.savefig), setting titles, and creating subplot layouts. Seaborn functions accept an ax parameter to draw on existing axes.

What's the difference between Seaborn and Plotly?

Seaborn produces static statistical graphics with beautiful defaults. Plotly creates interactive web-based charts. Use Seaborn for reports, papers, and Jupyter notebooks.

Why are my Seaborn plots not showing colors correctly?

Check the palette parameter. Seaborn supports named palettes (deep, muted, bright, pastel, dark, colorblind) and continuous ones (viridis, magma, coolwarm).

How do I save a Seaborn figure?

Use plt.savefig('plot.pdf', bbox_inches='tight'). If you created a FacetGrid or PairGrid, use figure.savefig() or access the underlying figure with .fig attribute.

Originally published on Ayodhyyya. Last updated June 1, 2026.