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
- Annotate values; use cmap="coolwarm", vmin=-1, vmax=1 and center=0.
- Chart: title "Board games"; x "Variable"; y "Variable".
Your inputs
| game | genre | minutes | rating | players |
|---|---|---|---|---|
| Orbit | Strategy | 20 | 4.1 | Two |
| Tiles | Puzzle | 15 | 3.8 | Solo |
| Quest | Adventure | 60 | 4.7 | Two |
| Grove | Strategy | 35 | 4.4 | Solo |
| Castle | Adventure | 45 | 4.5 | Two |
| Cards | Puzzle | 10 | 3.6 | Solo |
Remember the idea
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.
| Code or choice | Meaning |
|---|---|
corr(numeric_only=True) | Calculate the matrix before plotting it. |
vmin=-1, vmax=1, center=0 | Use the full correlation range with a neutral midpoint. |
annot=True | Print 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()