Heatmaps and colorbars
3.11.2
Display a numerical matrix and report how values map to color.
On this page
Map a matrix to pixels
imshow displays a regular matrix as an image. Add a colorbar to describe the scale. Label both axes so rows and columns retain their meaning.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(layout="constrained")
image = ax.imshow([[4, 7, 5], [8, 6, 3]], cmap="viridis", vmin=0, vmax=10)
ax.set_xticks([0, 1, 2], ["Mon", "Tue", "Wed"])
ax.set_yticks([0, 1], ["Store A", "Store B"])
fig.colorbar(image, ax=ax, label="Units")
plt.show()
Download image
Use the same scale for comparisons
Comparable heatmaps need a shared normalization or the same vmin and vmax. Select a diverging scale when deviations on both sides of a meaningful midpoint matter.