Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 65/ 160
04. Data Visualization & BI

Scatter Plots, Pair Plots & Heatmaps

Explore multivariate relationships with scatter, pair grids, and correlation heatmaps.

BeginnerScatter · Pair Plot3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Scatter plots show how two things move together, pair plots do it for many pairs, and heatmaps use colours to show which pairs move together most strongly.

Think of it like…

A heatmap is the map of a city's main roads at a glance; a scatter plot is street view of one road: the map tells you where to look, the street view tells you what is really there.

Where you’ve seen it

A churn model has 40 numeric features. A correlation heatmap would have 40 × 40 = 1,600 cells but only 780 unique pairs (40 × 39 / 2), so the analyst masks the upper triangle, uses a diverging colormap centred at zero (vmin=-1, vmax=1, center=0) and clusters rows and columns (sns.clustermap) to group related features. It reveals that tenure_months and total_charges correlate at 0.83 and monthly_charges and data_gb at 0.71. A scatter plot of total_charges against tenure_months with transparency shows a clean fan shape (variance grows with tenure), motivating dropping one of the pair or using a log transform, and a pair plot of the six key features (36 panels, 15 unique pairs) confirms there are no hidden curved relationships to the churn flag coloured as hue.

Words you’ll hear

Overplotting
Points hiding each other in dense regions; fixed with transparency, hexbin or density views.
Pair plot
A grid of scatter plots for every pair of variables, with distributions on the diagonal.
Pearson vs Spearman
Linear correlation versus rank-based correlation, which captures monotonic relationships.
Diverging colormap
A palette centred on a neutral midpoint (zero) so positive and negative correlations are distinct.

Visual diagram

Loading interactive diagram

Theory & deep dive

Scatter plots and pair plots

A scatter plot maps two numeric variables to position, the most accurate encoding, and can add a third or fourth through hue, size or style (sparingly). Look for direction and strength, curvature, clusters, heteroscedasticity (spread that changes with x), outliers and leverage points, and ceilings or floors from truncated measurement. Add a smoother (sns.regplot, lowess=True) with care: a fitted line draws the eye and can imply more than the data supports. With many points, overplotting hides density: use alpha=0.1, hexbin or 2D kdeplot, sample a subset, or aggregate with Datashader for millions of points.

A pair plot (sns.pairplot(df[cols], hue="churn", corner=True)) draws k² panels for k variables (k(k − 1)/2 unique pairs) with histograms or KDEs on the diagonal; corner=True drops the redundant upper triangle. It is practical for up to about 8 to 10 variables; beyond that it is unreadable and slow, so select variables first using the heatmap or domain knowledge. Colour by a target class to see separability, but beware that colour overplotting can mislead; facet or use transparency when classes overlap.

Panels grow quadratically

6 variables → 36 panels (15 unique pairs); 10 variables → 100 panels (45 unique); 40 variables → 1,600 panels (780 unique), which is why you triage with a heatmap before drawing pair plots.

Key takeaway: Use scatter plots to see shape, handle dense data with transparency or hexbin, and limit pair plots to a short feature list.

Correlation heatmaps done right

Compute corr = df.corr(numeric_only=True) (Pearson by default; method="spearman" for rank-based, robust to monotonic nonlinearity and outliers; "kendall" for small samples). Draw with sns.heatmap(corr, mask=np.triu(np.ones_like(corr, bool)), cmap="vlag", center=0, vmin=-1, vmax=1, annot=len(corr) < 15). Use a diverging palette centred at zero, fix the colour limits to −1 and 1 so different heatmaps are comparable, mask the redundant half and order variables by clustering (sns.clustermap) so related features sit together. Avoid rainbow palettes and annotate only when the grid is small enough to read.

Remember what the numbers mean: Pearson's r measures linear association only, is sensitive to outliers and says nothing about causation. A near-zero r can hide a strong U-shaped relationship, and a high r can come from a single extreme point or a confounder. Anscombe's quartet has four datasets with identical mean of x (9), mean of y (7.5) and r ≈ 0.816 but a linear trend, a curve, a line with one outlier and a vertical cluster with one leverage point. Large heatmaps also invite multiple-comparison problems: with 780 pairs, some will look notable by chance, so treat them as hypotheses, and check missing-data handling (pairwise versus listwise deletion changes coefficients).

Same r, different story

In Anscombe's quartet all four datasets give r ≈ 0.816, yet only the first is a clean linear relationship. Plotting them shows a parabola, an outlier-driven line and a single leverage point, exactly what the heatmap cell would hide.

Key takeaway: Use diverging, fixed-scale, masked and clustered heatmaps for triage, then verify with scatter plots and rank correlations.

How it works, step by step

Step 1 of 4
Analyst

1. Compute correlations

Run df.corr with Pearson and Spearman; note how missing values are handled.

40 features → 780 pairs
Key numbers at this step
Compare
Pearson vs Spearman

When to use it & trade-offs

What it is

Scatter plots, pair plots and correlation heatmaps are the core tools for exploring relationships among numeric variables. A scatter plot shows one pair (x, y) as points, revealing direction, strength, curvature, clusters and outliers; a pair plot (scatter matrix) draws every pair of a few variables at once with distributions on the diagonal; a correlation heatmap summarises many pairwise coefficients as a colour grid. They show patterns that a single number such as Pearson's r cannot, and each has limits: overplotting in scatter plots, quadratic growth in pair-plot panels and the fact that a correlation coefficient measures only linear association.

Why it matters: Feature selection, multicollinearity checks and hypothesis generation all start with relationships between variables. A heatmap highlights candidate relationships among 30 features in one glance, but only scatter plots show whether those relationships are linear, curved, clustered or driven by outliers. Anscombe's quartet is the canonical warning: four very different datasets share the same mean, variance and correlation (r ≈ 0.816), so always look before trusting a coefficient.

When to reach for it

  • Checking multicollinearity before regression
  • Choosing features and transforms
  • Looking for clusters and class separability

When to avoid or be careful

  • Trusting r without looking at the scatter
  • Pair plots of 30+ variables
  • Rainbow colormaps for correlation

Relationship views

Scatter plot
Best for: Shape of a single relationship

Strengths: Shows curvature, clusters and outliers.

Watch out: Overplots with big data; one pair at a time.

Pair plot
Best for: A handful of variables at once

Strengths: Includes distributions and class separation.

Watch out: k² panels; unreadable beyond about 10 variables.

Correlation heatmap
Best for: Triage across many variables

Strengths: Compact; spots collinearity.

Watch out: Linear summary only; hides shape.

Interview signals & numbers to know

When this topic comes up

  • “Which features are correlated?”
  • “Multicollinearity”
  • Anscombe's quartet
  • Overplotting
  • Spearman versus Pearson

Benchmarks & rules of thumb

k(k − 1)/2 -
unique pairs among k variables
r ≈ 0.816 -
shared by all four Anscombe datasets
±1 -
fixed limits for comparable correlation heatmaps

Common mistakes & follow-ups

Common interview mistakes

  • Reading a zero correlation as no relationship when the curve is U-shaped.
  • Using a sequential palette so negative and positive correlations look alike.
  • Treating dozens of apparent correlations as findings without correcting for multiple looks.

Likely interviewer follow-ups

  • Why look at a scatter plot when you have the correlation?
  • How do you visualise a relationship with a million points?
  • When would you use Spearman instead of Pearson?
Interview tip: Cite Anscombe's quartet and the fixed ±1 diverging heatmap. Together they show you know both the power and the limits of correlation views.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Scatter Plots, Pair Plots & Heatmaps with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Histograms, KDE & Box/Violin Plots
Next topic
Interactive Charts with Plotly