Multi-Plot Grids (FacetGrid, lmplot)
Seaborn lets you create multiple subplots based on categories, which makes it easier to compare patterns across groups.
Two 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 define:
- rows: variable that splits plots vertically
- cols: variable that splits plots horizontally
- hue (optional): variable that colors data points
This helps you break a complex dataset into smaller and clearer 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
combines a regression plot with a FacetGrid
.
It shows the relationship between variables along with a fitted trend line, and it can split the data by category.
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 FacetGrid when you want a flexible grid and plan to map different plot functions (scatter, hist, KDE, bar).
- Use lmplot when you want regression lines with optional faceting and coloring in one step.
Tips for Clear Multi-Plot Grids
- Limit the number of facets: Too many categories can make the grid hard to read.
- Keep axis ranges consistent: Shared scales make comparisons easier (
sharex
,sharey
). - Add legends and titles: Use
add_legend()
andmargin_titles=True
for clarity.
Now explore these patterns step by step in the notebook on the right side of the screen.
Quiz
0 / 1
What is the purpose of using a FacetGrid in Seaborn?
A FacetGrid is used to create based on the unique values of one or more categorical variables.
a single plot
multiple subplots
a pie chart
an interactive dashboard
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help