Multi-Plot Grids (FacetGrid, lmplot)
Seaborn lets you build multi-plot grids by faceting data into categories, making side-by-side comparisons easy.
Two figure-level tools are especially helpful for this: FacetGrid and lmplot.
FacetGrid Overview
A FacetGrid lays out a grid of subplots using the unique values of one or more categorical variables.
You can set:
row: split plots verticallycol: split plots horizontallyhue(optional): color by category
This breaks a complex dataset into smaller, comparable views.
FacetGrid with Scatter Plots
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, row="sex", col="time", hue="smoker", margin_titles=True)
g.map_dataframe(sns.scatterplot, x="total_bill", y="tip")
g.add_legend()
plt.show()
FacetGrid with Histograms
g = sns.FacetGrid(tips, col="day")
g.map_dataframe(sns.histplot, x="total_bill", bins=20)
plt.show()
lmplot Overview
lmplot is a figure-level function that combines a regression plot with FacetGrid faceting.
It fits a trend line and can facet and color by category in a single call.
lmplot with Categories and Facets
sns.lmplot(
data=tips,
x="total_bill",
y="tip",
hue="sex",
col="time",
height=4,
scatter_kws={"alpha": 0.6}
)
plt.show()
When to Use Each
- Use
FacetGridfor a flexible grid where you’ll map different plot functions (scatter, hist, KDE, bar). - Use
lmplotfor regression lines with optional faceting and coloring in one step.
Tips for Clear Multi-Plot Grids
- Limit facets: too many categories make grids hard to read.
- Keep axes consistent: shared scales (
sharex,sharey) make comparisons easier. - Add legends and titles: use
add_legend()andmargin_titles=Truefor clarity.
Now explore these patterns step by step in the notebook on the right side of the screen.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.