Heatmaps & imshow

Matplotlib is a Python library for creating charts and visualizations — and with imshow() it can paint a 2D grid of numbers as a grid of colors called a heatmap.

Learn Heatmaps & imshow in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll turn arrays into heatmaps, add a colorbar legend, label the axes, and annotate each cell with its value.

imshow ("image show") treats a 2D array like an image: each number becomes one colored square. A colorbar turns that color scale into a readable legend.

What you'll see: a 5×5 checkerboard of colors ranging from dark purple (low values near 0) through green to bright yellow (high values near 1). The colorbar on the right maps each shade back to its numeric value.

Real heatmaps have meaningful axis labels. Use plt.xticks() and plt.yticks() to replace the default 0, 1, 2… with category names, and switch the cmap to fit your data.

What you'll see: a 3×3 grid where the diagonal (each variable with itself, 1.0) is the deepest red. The "Price"/"Size" cells are also warm because 0.8 is a strong correlation, while the cooler, bluer cells show weaker relationships. Both axes now read Price, Size, Age.

A heatmap shows patterns, but readers often want the exact numbers too. Loop over the grid and call plt.text() to print each value in the center of its square.

What you'll see: the same colored grid, but now each square has its number printed in white in the middle. The double loop visits every (row, col) position, and aspect="auto" lets the cells stretch to fill the figure rather than staying perfectly square.

Replace each ___ to draw a heatmap with a colorbar.

imshow needs a 2D array. Passing a flat 1D list won't work — reshape it or build a real grid first.

Dark text on dark cells disappears. Set color="white" (or pick a lighter colormap) so the numbers stand out.

Draw a heatmap of sales by region and month, with a colorbar and a title.

Lesson 17 complete — you can paint data as color!

You drew heatmaps with imshow, added colorbars, labeled the axes with categories, and annotated every cell with its value.

🚀 Up next: 3D Plots — lift your charts off the page into three dimensions.

Practice quiz

What does a heatmap display?

  • A 2D grid of numbers as a grid of colors
  • A line over time
  • A single bar per category
  • A pie of proportions

Answer: A 2D grid of numbers as a grid of colors. A heatmap encodes each cell's value as a color in a 2D grid.

Which function draws a 2D array as colored cells?

  • plt.heatmap()
  • plt.imshow()
  • plt.grid()
  • plt.matrix()

Answer: plt.imshow(). plt.imshow(data) treats a 2D array like an image, one colored pixel per value.

What does the cmap argument choose?

  • The cell size
  • The colormap (number-to-color mapping)
  • The number of bins
  • The aspect ratio

Answer: The colormap (number-to-color mapping). cmap selects the colormap that maps numbers to colors, e.g. 'viridis'.

Which call adds a legend mapping colors back to values?

  • plt.legend()
  • plt.key()
  • plt.colorbar()
  • plt.scale()

Answer: plt.colorbar(). plt.colorbar() adds the color-to-value legend strip beside the heatmap.

Which is Matplotlib's default, perceptually uniform colormap?

  • jet
  • gray
  • viridis
  • rainbow

Answer: viridis. 'viridis' is the default and is perceptually uniform.

For data ranging negative to positive (like correlations), a good colormap is...

  • A diverging map like 'coolwarm'
  • 'viridis'
  • 'gray'
  • 'hot'

Answer: A diverging map like 'coolwarm'. A diverging colormap such as 'coolwarm' centers neutral values on white.

How do you write each cell's value on top of its square?

  • plt.annotate_grid()
  • plt.text(col, row, value) in a loop
  • plt.label()
  • plt.cell_text()

Answer: plt.text(col, row, value) in a loop. Loop over rows and columns and call plt.text(col, row, value, ha='center', va='center').

Why does imshow raise 'Invalid shape for image data' on a flat 1D list?

  • The list is empty
  • imshow needs a 2D array, not a flat 1D list
  • cmap is missing
  • colorbar wasn't called

Answer: imshow needs a 2D array, not a flat 1D list. imshow needs a 2D array; reshape or build a real grid first.

Why might cell labels be invisible on a heatmap?

  • The colorbar overwrote them
  • Dark text on dark cells disappears
  • imshow hides text
  • plt.text() is deprecated

Answer: Dark text on dark cells disappears. Set color='white' (or use a lighter colormap) so dark cells don't hide the numbers.

How do you replace default 0,1,2 tick labels with category names?

  • plt.relabel()
  • plt.xticks(range(n), labels)
  • plt.names(labels)
  • plt.set_labels()

Answer: plt.xticks(range(n), labels). plt.xticks(range(n), labels) puts category names on the axis.