Correlation matrix heatmap
A correlation matrix heatmap shows the Pearson correlation between every pair of numeric columns, using a diverging colormap so positive and negative relationships are distinct and zero is neutral.
When to use it
- You explore a dataset with many numeric columns.
- You check for redundant (highly correlated) variables before modelling.
Watch out: Correlation only captures linear relationships. Look at a scatter matrix for the interesting pairs.
Ask for it in pltchat
Upload your file and type something like:
"correlation matrix of all numeric columns, diverging colours from -1 to 1"
pltchat writes the loader and the plot for your columns, runs it in your browser and shows you the code.
The matplotlib code
Self-contained with example data; swap in pd.read_csv(...) or
pd.read_excel(...) for your own file.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
rng = np.random.default_rng(13)
n = 300
temp = rng.normal(20, 5, n)
df = pd.DataFrame({"temp": temp,
"ice_cream": 3 * temp + rng.normal(0, 8, n),
"umbrellas": -2 * temp + rng.normal(0, 12, n),
"humidity": rng.normal(60, 10, n),
"visitors": 5 * temp + rng.normal(0, 30, n)})
corr = df.corr()
fig, ax = plt.subplots(figsize=(5.5, 4.5))
im = ax.imshow(corr, cmap="RdBu_r", vmin=-1, vmax=1)
ax.set_xticks(range(len(corr)), corr.columns, rotation=45, ha="right")
ax.set_yticks(range(len(corr)), corr.columns)
for i in range(len(corr)):
for j in range(len(corr)):
ax.text(j, i, f"{corr.iat[i, j]:.2f}", ha="center", va="center", fontsize=8)
fig.colorbar(im, ax=ax, label="Pearson r")
plt.tight_layout()
plt.show()
Upload a CSV, Excel, JSON or Parquet file. Your first plot is always free.