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 · VR3 · 12 MIN

Review · Explain the evidence

Task 1 · Retrieve and combine

Exercises within this concept

  1. Task 1Retrieve and combineCurrent exercise
  2. Task 2Retrieve and combine
  3. Task 3Retrieve and combine
Task 1 · Board games

Using df games lasting at least 20 minutes, show the correlation matrix for minutes and rating, in that order.

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

Your requirements

  1. Annotate values; use cmap="coolwarm", vmin=-1, vmax=1 and center=0.
  2. Chart: title "Board games"; x "Variable"; y "Variable".

Your inputs

Board games · 6 synthetic rows
gamegenreminutesratingplayers
OrbitStrategy204.1Two
TilesPuzzle153.8Solo
QuestAdventure604.7Two
GroveStrategy354.4Solo
CastleAdventure454.5Two
CardsPuzzle103.6Solo

Revisit the concept lesson →

Remember the idea

Concept sketch: signed correlations; diagonal always 1xyzx1.00.5-0.5y0.51.00.0z-0.50.01.0signed correlations; diagonal always 1

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

A small example

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

What each choice does
Code or choiceMeaning
corr(numeric_only=True)Calculate the matrix before plotting it.
vmin=-1, vmax=1, center=0Use the full correlation range with a neutral midpoint.
annot=TruePrint each cell’s value. cmap="vlag" or "coolwarm" selects a diverging palette.
Hint

Correlations must describe the selected population, with a consistent colour scale.

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))
selected = df[df["minutes"] >= 20]
sns.heatmap(
    selected[["minutes", "rating"]].corr(),
    annot=True,
    cmap="coolwarm",
    vmin=-1,
    vmax=1,
    center=0,
    ax=ax,
)
ax.set(title="Board games", xlabel="Variable", ylabel="Variable")
fig.tight_layout()
plt.show()
Your task · Task 1
  1. Using df games lasting at least 20 minutes, show the correlation matrix for minutes and rating, in that order.
  2. Annotate values; use cmap="coolwarm", vmin=-1, vmax=1 and center=0.
  3. Chart: title "Board games"; 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.