Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Visualise lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Explain the evidence · V22 · 8 MIN

Correlation heatmap

Show a correlation matrix on a fixed colour scale.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeAdapt a requirement
  3. TransferChoose and combine

Understand the idea

A correlation heatmap colours a matrix of pairwise numeric relationships. Keep a fixed colour scale so colours retain the same meaning.

Concept sketch: signed correlations; diagonal always 1xyzx1.00.5-0.5y0.51.00.0z-0.50.01.0signed correlations; diagonal always 1
Illustration · not the exercise output

A small example

A cell at row hours and column score contains their correlation. The mirrored score–hours cell contains the same value.

Follow the code

Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=(6, 4))
matrix = df.corr(numeric_only=True)
sns.heatmap(matrix, annot=True, vmin=-1, vmax=1, center=0, cmap="vlag", ax=ax)
ax.set(title="Study club", xlabel="Variable", ylabel="Variable")
fig.tight_layout()
plt.show()

What each part does

df.corr(numeric_only=True)
prepare the matrix
annot=True
print cell values
vmin=-1, vmax=1, center=0
meaningful colour limits

Your inputs

The editable setup on the right creates df. Run executes the setup and your work from top to bottom.

Study club · 8 synthetic rows
studentclubhoursscoregroup
AriArt152A
BoCode371A
CyArt265B
DeeCode482B
EliArt589A
FloCode376A
GusArt693B
HanCode261B

Your task · Follow

  1. Using df, plot sns.heatmap of numeric correlations with annot=True, vmin=-1, vmax=1, center=0 and cmap="vlag".
  2. Chart: title "Study club"; x "Variable"; y "Variable".
Hint

Build a correlation matrix first, then keep the colour scale fixed from -1 to 1.

Reveal solution

One way to do it. Keep any supplied setup in the editor and use this in the Your work section.

import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize=(6, 4))
matrix = df.corr(numeric_only=True)
sns.heatmap(matrix, annot=True, vmin=-1, vmax=1, center=0, cmap="vlag", ax=ax)
ax.set(title="Study club", xlabel="Variable", ylabel="Variable")
fig.tight_layout()
plt.show()
Your task · Follow
  1. Using df, plot sns.heatmap of numeric correlations with annot=True, vmin=-1, vmax=1, center=0 and cmap="vlag".
  2. Chart: title "Study club"; x "Variable"; y "Variable".

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Display charts with plt.show().

Python starts when you open a lesson.

Output

Run your code to see what Python returns.