Learning objectives
displot vs histplot).FacetGrid, PairGrid, combining Seaborn + Matplotlib.Plot categories and representative functions — 25 min
FacetGrid, PairGrid) — 8 minTwo main API styles:
displot, relplot, catplot, pairplot, lmplot.Axes) — scatterplot, histplot, boxplot, violinplot, heatmap, etc.import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
sns.__version__ # ensure recent version (0.11+ has many features)
# load example datasets
tips = sns.load_dataset("tips")
iris = sns.load_dataset("iris")
penguins = sns.load_dataset("penguins")
Data forms
long (tidy): each row = observation. e.g. tips (total_bill, tip, sex, smoker, day, time)wide: multiple related measurement columns (e.g., columns for each time point). Some seaborn functions accept wide-form directly.Themes & context
sns.set_theme(style="whitegrid", context="notebook")
# style: whitegrid, darkgrid, white, ticks, dark
# context: paper, notebook, talk, poster
# control rc parameters
sns.set(rc={"figure.figsize": (8, 5)})
Color palettes
sns.color_palette("deep")
sns.color_palette("crest", as_cmap=True)
# categorical vs sequential vs diverging
sns.palplot(sns.color_palette("tab10"))
We’ll list functions by purpose with short code examples.
Axes-level: scatterplot, lineplot.
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time", style="smoker")
sns.lineplot(data=some_time_series_df, x="date", y="value", hue="category")
Figure-level: relplot(kind="scatter" / "line") — convenience wrapper that returns FacetGrid.
sns.relplot(data=tips, x="total_bill", y="tip", col="day", hue="sex", kind="scatter")
Notes: scatterplot accepts size, hue, style for multidimensional encoding. Use alpha and s for density/marker size.
stripplot, swarmplot (jittered points)boxplot, violinplot (distribution summaries)barplot (aggregate with CI), countplot (counts)pointplot (point estimates with CI)sns.boxplot(data=tips, x="day", y="total_bill")
sns.violinplot(data=tips, x="day", y="total_bill", hue="sex", split=True)
sns.barplot(data=tips, x="day", y="total_bill", estimator=sum)
sns.countplot(data=tips, x="day")
Figure-level: catplot(kind=...) — good for faceting.
sns.catplot(data=tips, x="day", y="tip", kind="violin", hue="sex", col="time")
When to use: use boxplot for robust summaries, violinplot for showing full distribution, strip/swarm for raw points.
histplot, kdeplot, ecdfplot, displot (figure-level combining hist/kde)sns.histplot(data=iris, x="sepal_length", kde=True)
sns.kdeplot(data=iris, x="sepal_length", hue="species", fill=True)
sns.displot(data=iris, x="sepal_length", col="species", kde=True)
Notes: displot returns a FacetGrid so you can facet easily.
regplot (axes-level), lmplot (figure-level + faceting)sns.regplot(data=tips, x="total_bill", y="tip", scatter_kws={"s": 10})
sns.lmplot(data=tips, x="total_bill", y="tip", hue="smoker", col="day")
Options: lowess smoothing, polynomial order, ci for confidence intervals.
heatmap (matrices/correlations)clustermap (hierarchical clustering heatmap)corr = iris.corr()
sns.heatmap(corr, annot=True, fmt='.2f', square=True)
sns.clustermap(iris.select_dtypes('number'), metric='euclidean', method='average')
Notes: Good for showing variable correlation structures or distance matrices.
pairplot (quick pairs), PairGrid (customizable)jointplot, JointGrid for bivariate + marginal distributionssns.pairplot(iris, hue="species", corner=True)
sns.jointplot(data=iris, x="sepal_length", y="sepal_width", kind="hex")
When to use: exploratory data analysis to inspect pairwise relationships and marginal distributions.
FacetGrid is the core: map plotting functions across facets (rows, cols, hue).g = sns.FacetGrid(tips, col="time", row="smoker", margin_titles=True)
g.map(sns.scatterplot, "total_bill", "tip")
relplot, catplot, displot, lmplot, pairplot are built-in figure-level functions that create and use a FacetGrid internally.Tips: Use sharex/sharey to control axis sharing; use col_wrap to wrap many facets into rows.
Figure-level vs Axes-level
plt.subplots()), use axes-level functions and pass ax= or operate on ax.Long vs Wide data
pd.melt() to reshape.Combining with Matplotlib
fig, ax = plt.subplots()
sns.boxplot(data=tips, x="day", y="total_bill", ax=ax)
ax.set_title("Total bill by day")
Controlling aesthetics
sns.set_theme() is recommended.palette argument for color control.legend/ax.legend() to relocate or format legends.Performance
alpha, s, or hex/bin plots: sns.histplot(..., bins=100, pthresh=0.05, cmap='mako') or kind='hex' in jointplot.Statistical interpretation
estimator and ci mean in barplot/pointplot — defaults are mean and bootstrapped 95% CI.Exercise 1 (Exploration, 15–30 min)
penguins or tips data.pairplot colored by species/sex.Exercise 2 (Figure composition)
heatmap of correlations for numeric variables. Top-right: boxplot by category. Bottom row: scatterplot with regression line.Exercise 3 (Communication)
tips, make a single publication-quality figure that shows how tip varies by day and time, and includes raw points (jitter), a violin or box for distribution, and a summary stat (mean) as points.Relational: scatterplot, lineplot, relplot
Categorical: catplot, boxplot, violinplot, barplot, countplot, stripplot, swarmplot, pointplot
Distribution: histplot, kdeplot, ecdfplot, displot
Regression: regplot, lmplot
Pairwise / Joint: pairplot, PairGrid, jointplot, JointGrid
Matrix: heatmap, clustermap
Utilities: load_dataset, set_theme, color_palette, palplot, despine
# Quick exploratory EDA
import seaborn as sns
sns.set_theme(style='whitegrid')
# 1. Pairplot
sns.pairplot(sns.load_dataset('iris'), hue='species')
# 2. Faceted scatter
sns.relplot(data=sns.load_dataset('tips'), x='total_bill', y='tip', col='day', hue='sex')
# 3. Combined categorical plot
ax = sns.violinplot(data=tips, x='day', y='total_bill', inner=None)
sns.stripplot(data=tips, x='day', y='total_bill', color='k', size=3, jitter=True, ax=ax)
# 4. Heatmap of correlations
import numpy as np
corr = sns.load_dataset('penguins').select_dtypes('number').corr()
sns.heatmap(corr, annot=True, cmap='vlag')