Skip to content

Heatmaps for Correlation

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

Correlation heatmap
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()
Correlation heatmap
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.

Next

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

🧪 Try It Yourself

Exercise 1 – Compute a Correlation Matrix

Exercise 2 – Draw the Heatmap With Numbers

Exercise 3 – Fix the Color Scale

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did