Plotting a 2D heatmap
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 2D heatmap displays a matrix of values by mapping numbers to colors. It is useful when the structure of the data matters as much as the individual values, such as correlation matrices, confusion matrices, grid simulations, and image-like arrays.
The key decisions are usually not "how do I draw colored squares" but "what does each axis mean" and "how should the color scale be interpreted."
A Basic Heatmap with Matplotlib
For matrix-shaped data, matplotlib.pyplot.imshow is the most direct starting point.
imshow treats the input as an image-like grid. The colorbar is important because it explains what the colors mean numerically.
Labeling Axes Clearly
If your rows and columns have names, add tick labels so the heatmap is interpretable:
Without labels, a heatmap often looks impressive but explains very little.
Annotating Cell Values
For smaller matrices, adding text values inside each cell can make the chart much easier to read:
This works well for dashboards, reports, and confusion matrices with a manageable number of cells.
When Seaborn Is More Convenient
If you want labels, annotations, and a polished default style with less code, Seaborn is often more convenient:
Seaborn is especially pleasant when the data already lives in a Pandas DataFrame.
Choosing a Good Color Scale
Colormap choice affects interpretation. Sequential colormaps such as viridis or Blues are good for values that go from low to high. Diverging colormaps are better when the midpoint matters, such as positive versus negative deviations around zero.
It also helps to set vmin and vmax deliberately. Otherwise, each plot may auto-scale differently, making side-by-side comparison misleading.
For example:
That is appropriate for values centered around zero, such as residuals or correlations.
Common Pitfalls
The most common problem is plotting the matrix without a colorbar. Without a legend, viewers can see the pattern but not the meaning of the colors.
Another issue is using the wrong aspect ratio. A square matrix usually looks best with square cells, while rectangular business tables may need aspect="auto" so labels fit cleanly.
Developers also sometimes choose colormaps with poor contrast or misleading perceptual jumps. A heatmap is not only decoration; the color encoding is the data.
Finally, be careful with very large matrices. A dense heatmap with thousands of rows and columns becomes unreadable. In those cases, consider aggregation, clustering, or interactive zooming.
Summary
- A 2D heatmap maps matrix values to colors and works best when both axes are meaningful.
- '
imshowis the simplest Matplotlib entry point for matrix-like data.' - Add labels, annotations, and a colorbar to make the plot interpretable.
- Use Seaborn when you want a cleaner high-level API for labeled data frames.
- Choose colormaps and value ranges deliberately so the color scale communicates the right story.
Related reading
- Plotting a list of x, y coordinates
- Plotting a pie chart out of a dictionary
- Plotting a ROC curve in scikit yields only 3 points
- Plotting decision boundary for High Dimension Data
- Plotting in a non-blocking way with Matplotlib
- Plotting numpy array using Seaborn
- plotting spectrogram in audio analysis
- Plotting time on the independent axis
.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.