Python Central HubSign in

Data Analytics with Python / Phase 6: Statistical Visualization with Seaborn

Heatmaps for Correlation

Plot correlation matrices and interpret them safely using Seaborn heatmap.

Correlation recap

Correlation describes how two numeric variables move together.

  • +1: strong positive relationship
  • -1: strong negative relationship
  • 0: no linear relationship
diagram From a DataFrame to a heatmap mermaid
A correlation matrix is a square grid of numbers, which sns.heatmap turns into a grid of colors.

Correlation heatmap

Python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
 
penguins = sns.load_dataset("penguins").dropna()
 
corr = penguins[[
    "bill_length_mm",
    "bill_depth_mm",
    "flipper_length_mm",
    "body_mass_g",
]].corr()
 
plt.figure(figsize=(6, 4))
sns.heatmap(corr, annot=True, cmap="coolwarm", vmin=-1, vmax=1)
plt.title("Penguins correlation")
plt.tight_layout()
plt.show()

Visualize it

Each cell's color encodes a correlation value from -1 to +1 — amber for strong positive, blue for strong negative, and dim in the middle for near zero:

sketch Correlation matrix as a color grid p5.js
Each cell's shade encodes its correlation value: amber for positive, blue for negative, dim for near zero.

Tips

  • Correlation is not causation.
  • Outliers can inflate correlations.
  • For non-linear relationships, correlation can be misleading.

Check your understanding

quizCheck yourself
  1. What does a correlation of -1 mean?

    Show answer

    C — Strong negative relationship — +1 is a strong positive relationship, -1 a strong negative one, and 0 means no linear relationship.

  2. Why does the lesson set vmin=-1, vmax=1 in sns.heatmap?

    Show answer

    B — To keep the color scale comparable across plots — Without fixed limits the colormap can auto-scale, making a weak correlation look as hot as a strong one.

  3. What does annot=True do?

    Show answer

    A — Writes each correlation value in its cell — annot=True prints the numbers inside the cells so you can read exact values.

  4. Which is a warning the lesson gives about correlation?

    Show answer

    D — Outliers can inflate it, and it misses non-linear patterns — Correlation is not causation, outliers can inflate it, and non-linear relationships can fool it.

Next

Continue to Pair Plots to see the raw scatter relationships behind each correlation number.

🧪 Try It Yourself

Exercise 1 – Compute a Correlation Matrix

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Draw the Heatmap With Numbers

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Fix the Color Scale

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did