Plot correlation matrix using pandas
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A correlation matrix is useful when you want a quick view of how numeric variables move together. In pandas, computing the matrix is easy with DataFrame.corr(). Plotting it usually means pairing pandas with a visualization library such as Matplotlib or seaborn. The important part is not just drawing the heatmap, but knowing which columns are being correlated and what kind of correlation you are asking for.
Compute the Correlation Matrix First
Start with the numeric data and call corr():
By default, pandas uses Pearson correlation, which measures linear association. This is often the expected starting point.
Plot with seaborn
The most common plotting approach is a heatmap:
This is a strong default because:
- color shows positive versus negative relationship
- '
annot=Trueprints the actual coefficient values' - fixed
vminandvmaxkeeps the scale comparable across plots
If seaborn is available, this is usually the cleanest answer.
Plot with Matplotlib Only
If you want a lighter dependency stack, Matplotlib alone also works:
This gives you full control, though it requires a bit more manual labeling.
Pick the Right Correlation Method
Pandas supports different correlation methods:
Use them intentionally:
- Pearson for linear relationships
- Spearman for monotonic rank relationships
- Kendall for ordinal-style association when robustness matters more than speed
Choosing the method is part of the analysis, not just a plotting option.
Use Only the Right Columns
Correlation is meaningful only for numeric variables. In mixed datasets, select the numeric columns explicitly:
This avoids accidental issues with text columns and makes the plot easier to interpret.
It also helps when the DataFrame contains identifier columns such as IDs or ZIP-like codes that are technically numeric but not analytically meaningful. Those should often be excluded before computing correlations.
Improve Readability on Larger Matrices
For wide datasets, a plain heatmap becomes cluttered. A few practical improvements are:
- increase figure size
- rotate axis labels
- round displayed values
- show only one triangle of the symmetric matrix
Example using a mask:
This reduces visual duplication because the upper and lower triangles contain the same information.
Common Pitfalls
- Plotting correlations for columns that are not numerically meaningful.
- Interpreting correlation as causation.
- Using Pearson correlation on data where only rank-based association makes sense.
- Forgetting to standardize the color scale and then comparing plots unfairly.
- Trying to annotate huge matrices where the labels become unreadable.
Summary
- Use
DataFrame.corr()to compute the matrix before plotting. - seaborn's
heatmapis the most common and readable plotting approach. - Choose the correlation method intentionally instead of relying on defaults blindly.
- Select numeric and meaningful columns before computing correlations.
- For large matrices, mask one triangle and improve label formatting to keep the plot readable.
Related reading
- Plot custom data with Tensorboard
- Plot decision tree in R Caret
- Plot feature importance with xgboost
- Plot Interactive Decision Tree in Jupyter Notebook
- Plot multiple graphs in one plot using Tensorboard
- Plot PCA loadings and loading in biplot in sklearn like R's autoplot
- Plot k-Nearest-Neighbor graph with 8 features?
- Plot learning curves with caret package and R
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.