Why pair plots?
Pair plots are a fast way to inspect:
- Pairwise relationships between variables
- Distributions on the diagonal
- Group separation using
hue
They are great early in EDA.
flowchart TD A["n numeric columns"] --> B["Build an n x n grid"] B --> C["Off-diagonal cells:
scatter of column i vs j"] B --> D["Diagonal cells:
histogram or KDE of column i"]
Basic pairplot
import seaborn as sns
penguins = sns.load_dataset("penguins").dropna()
sns.pairplot(penguins[[
"bill_length_mm",
"bill_depth_mm",
"flipper_length_mm",
"body_mass_g",
]])Pairplot with hue
import seaborn as sns
penguins = sns.load_dataset("penguins").dropna()
sns.pairplot(
penguins,
vars=["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"],
hue="species",
)Tips
- Pair plots can be slow for large datasets. Sample rows if needed.
- Use
hueto check if groups separate naturally.
Check your understanding
What does sns.pairplot draw?
A pair plot is a scatter plot matrix, with each variable's distribution along the diagonal.
Show answer
B — A matrix of pairwise scatter plots with distributions on the diagonal — A pair plot is a scatter plot matrix, with each variable's distribution along the diagonal.
Why add hue="species" to the penguins pairplot?
Coloring by species shows whether groups form distinct clusters.
Show answer
C — To check whether groups separate naturally — Coloring by species shows whether groups form distinct clusters.
The pairplot is slow on a very large dataset. What does the lesson advise?
Many variables times many rows means many scatter plots, so sample rows first.
Show answer
A — Sample rows — Many variables times many rows means many scatter plots, so sample rows first.
Which option gives smooth curves on the diagonal?
diag_kind="kde" replaces the diagonal histograms with density curves. plot_kws only styles the scatter plots.
Show answer
B — diag_kind="kde" — diag_kind="kde" replaces the diagonal histograms with density curves. plot_kws only styles the scatter plots.
Next
Continue to Facet Grids to split any plot type into small multiples by category.
🧪 Try It Yourself
Exercise 1 – A Basic Pair Plot
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Color by Species
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Use KDE on the Diagonal
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.