Seaborn Tutorial: A Guide to Statistical Charts in Python

A map of the Seaborn tutorials: which function to use, distributions, categories, relationships, heatmaps, facets, themes, and saving.

Seaborn draws statistical charts from a pandas DataFrame: pass data= and name the columns for x, y and hue, as in sns.scatterplot(data=df, x="price", y="sales", hue="region"). It is built on Matplotlib, so you can adjust the result with Matplotlib methods. These tutorials are grouped by task, and newer ones are written for Seaborn 0.13.

Grid of twelve Seaborn charts: histogram, density, line, regression, pair plot, clustermap, heatmap, swarm plot, ridge plot, joint plot, count plot and themes

Quick Example

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

df = pd.DataFrame({
    "week": list(range(1, 9)) * 2,
    "plan": ["Free"] * 8 + ["Paid"] * 8,
    "signups": [120, 132, 128, 151, 160, 158, 175, 190,
                40, 46, 55, 58, 71, 80, 86, 99],
})

sns.set_theme(style="whitegrid")
fig, ax = plt.subplots(figsize=(7, 4.5))
sns.lineplot(data=df, x="week", y="signups", hue="plan", marker="o", ax=ax)
ax.set_title("Weekly Signups by Plan")
plt.show()

Seaborn line chart in which free signups rise from 120 to 190 and paid signups from 40 to 99 over eight weeks

Where Should I Start With Seaborn?

Step Question Tutorial
1. Basics What does Seaborn do? Intro to Seaborn
2. Two kinds of function Why is ax= or figsize= ignored? Figure-level vs axes-level functions
3. Look How do I change the style of every chart? set_theme, styles and contexts
4. Legend How do I move it? sns.move_legend
5. Export How do I save a sharp image? High-resolution figures

Which Seaborn Plot Should I Use?

To choose by appearance, browse the Python Chart Gallery.

To show Function Tutorial
One distribution sns.histplot, sns.displot Histograms
Smoothed distributions sns.kdeplot Density plots
Many distributions stacked sns.FacetGrid with kdeplot Ridge plots
Distribution shape by group sns.violinplot Violin plots
Every point by group sns.swarmplot Beeswarm plots
Points and a summary together sns.boxplot and sns.swarmplot Box plot with swarm
Rows per category sns.countplot Count plots
A value by category and group sns.barplot(hue=...) Grouped bars
A trend over time sns.lineplot Line plots
A fitted relationship sns.regplot, sns.lmplot Regression plots
Two variables and their distributions sns.jointplot Joint plots
Every pair of columns sns.pairplot Pair plots
A grid of values sns.heatmap Heatmaps
A grid with similar rows grouped sns.clustermap Clustermaps

Customize Seaborn Charts

You want to Tutorial
Print values on bars Bar plot value labels
Reorder categories Category order
Change or remove the shaded interval Confidence interval bands
Use your own colors Custom color palettes
Annotate or mask heatmap cells Styling heatmaps

Facets, Figure Size, and Titles

You want to Tutorial
Resize a catplot catplot size and aspect ratio
Title a grid and its panels FacetGrid titles
Rotate labels in a grid Rotate FacetGrid and pairplot labels

Should I Use Seaborn, Matplotlib, or the Objects Interface?

Choose When
Seaborn functions Your data is in a DataFrame and you want grouping, aggregation or intervals handled for you
Matplotlib You need precise control or a chart Seaborn doesn't provide
seaborn.objects You like building a chart in layers of marks, stats and moves
Plotly Readers need to hover, zoom or filter

Common Seaborn Problems

Problem Fix Details
catplot ignores figsize Use height and aspect catplot size
A figure-level function ignores ax= Use the axes-level version, such as histplot for displot Figure vs axes level
The legend covers the data sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1)) Move the legend
The grid title overlaps the panels g.figure.suptitle("Title", y=1.02) FacetGrid titles
"Points cannot be placed" warning Reduce size, widen the figure or use stripplot Beeswarm plots
The ci parameter is deprecated Use errorbar Confidence bands

Similar Topics