Heatmap

Heatmaps turn numerical data into a visual pattern by representing values with colors. Instead of reading each number in a table, we can quickly spot regions with high or low values, compare groups, and identify patterns. Heatmaps are especially useful for displaying matrices, such as correlation tables, measurements collected across a grid, or values that vary across two dimensions.

In Python, Matplotlib’s plt.imshow() provides a simple way to display a two-dimensional array as a heatmap. Each array element becomes a colored cell: its row and column determine its position, and its value determines its color. A colormap controls how numerical values map to colors, while a colorbar provides a reference for interpreting them.

For example, we can visualize a small array as follows:

import pandas as pd 
import matplotlib.pyplot as plt

data = pd.DataFrame([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

plt.imshow(data, cmap="viridis", interpolation="nearest")
plt.colorbar(label="Value")
plt.xlabel("Column")
plt.ylabel("Row")
plt.title("A Simple Heatmap")
plt.show()

Here, cmap="viridis" selects the color scheme, and interpolation="nearest" keeps the individual cells clearly visible. By default, imshow() places the first row at the top of the plot. Setting origin="lower" places it at the bottom instead.

A useful heatmap needs a clear color scale and meaningful labels. When comparing several heatmaps, setting the same vmin and vmax ensures that the same color represents the same value in every plot. These choices help make the visualization both readable and accurate.