Keep a correlation matrix honest about missing pairs, redundant cells and feature order.
Correlation heatmaps: show pairwise coverage before interpreting color
The matrix has more than coefficients
A correlation cell describes observations shared by two variables. With missing fields, one cell may use 810 records and another 47; identical color saturation hides that difference. Carry a second matrix of pairwise counts and expose it in labels, tooltips or a companion table. A blank pair means insufficient shared records or an undefined statistic, not zero association. Missing-cell rules distinguish these states. Record which rows passed eligibility filters before calculating either matrix.
Choose a missing-data contract
Pairwise deletion uses every available pair but can give each cell a different population. Complete-case deletion holds one population constant but may discard most records and change who remains. State the choice and counts. A constant column has zero variance and no Pearson correlation; mark it undefined. Do not fill its cell with zero merely to complete a square. Dataset grain and join checks should precede calculation so duplicated rows do not inflate confidence.
Order features for the reading task
Alphabetical order is stable and auditable. A domain order groups related measures. Data-driven clustering can reveal blocks, but the clustering method and input matrix become part of the result; changing order between periods makes comparison harder. Fix order for side-by-side charts, or record the exact order for each export. Show a legend centered on zero with identical limits in every panel. Scale integrity prevents a weak coefficient from receiving the same shade as a strong one elsewhere.
Do not infer a mechanism
A large coefficient can arise from shared time trends, repeated entities, selection or a third variable. The matrix is a screen for questions, not proof that changing one field changes another. Confirm striking cells with paired scatter plots, units and source counts; outliers can dominate a linear coefficient. Avoid painting both triangles when space is tight, since the duplicate area adds no information. Dense scatter views help inspect what the matrix compressed. Describe the population and time range beside every matrix.
Worked contract
from math import sqrt
def pearson_pair(records: list[dict[str, float | None]],
left: str, right: str) -> tuple[float | None, int]:
pairs = [(record[left], record[right]) for record in records
if record.get(left) is not None and record.get(right) is not None]
count = len(pairs)
if count < 2:
return None, count
left_mean = sum(value[0] for value in pairs) / count
right_mean = sum(value[1] for value in pairs) / count
cross = sum((x - left_mean) * (y - right_mean) for x, y in pairs)
left_square = sum((x - left_mean) ** 2 for x, _ in pairs)
right_square = sum((y - right_mean) ** 2 for _, y in pairs)
if not left_square or not right_square:
return None, count
return cross / sqrt(left_square * right_square), count
shipments = [{"distance": 47, "cost": 81},
{"distance": 59, "cost": 97},
{"distance": 71, "cost": 113},
{"distance": 83, "cost": None}]
coefficient, paired_count = pearson_pair(shipments, "distance", "cost")
assert round(coefficient, 12) == 1 and paired_count == 3Performance and operating cost
For P fields and N rows, a direct full matrix performs O(P²N) pair work and stores O(P²) coefficients and counts. Columnar execution can lower constant costs, but pairwise coverage still needs explicit accounting. The example returns undefined for insufficient pairs or constant values and does not provide a significance test. A precise coefficient on three shared rows is weak evidence for a broad population claim.
Common Mistakes
- Showing a coefficient without its pairwise record count.
- Coloring undefined correlations as zero.
- Changing row order across comparison periods without notice.
- Treating a strong correlation as a causal intervention result.
Validation drill
Build a five-field matrix where one field appears in only 47 of 810 records. Show the count matrix beside the coefficient matrix, then compare pairwise and complete-case populations.
Read next
- Heatmaps: make missing cells, rates and color domains explicit
- Dense scatterplots: reveal overlap without hiding rare observations
- Axis scales, baselines and labels: prevent a correct number from telling a false story
- Dashboard filters and provenance: make every view reproducible
