Displaying Images & Matrices (imshow)
imshow renders a 2D array as a grid of colored pixels, turning a matrix, heatmap, or image into a picture where each cell's value becomes a color.
Learn Displaying Images & Matrices (imshow) in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise…
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 display arrays with ax.imshow() , control origin , extent , aspect , and interpolation , compare it with pcolormesh , and add a colorbar.
Hand ax.imshow() any 2D array and it paints one colored pixel per cell, mapping values to colors through the colormap. It returns a mappable, so you can attach a colorbar just like with the heatmaps you saw earlier. This is the simplest way to see the contents of a matrix.
What you'll see: a 3-row, 4-column grid of squares shaded from dark purple (1) to bright yellow (12), with a colorbar translating each shade back into its number.
By default imshow uses origin="upper" , putting row 0 at the top — the image convention. Switch to origin="lower" to put row 0 at the bottom , matching the usual math orientation where y grows upward. Getting this right keeps your data from appearing upside-down.
What you'll see: the same 4×4 gradient twice. On the left the brightest cell (15) sits in the bottom-right with the count rising downward; on the right the array is flipped vertically so the count rises upward.
Three more arguments give you full control. extent=[xmin, xmax, ymin, ymax] maps the array onto real data coordinates so the axes show units, not pixel indices. aspect="auto" lets the image stretch to fill the Axes (use "equal" for square pixels). interpolation smooths the pixels — "nearest" keeps them blocky, "bilinear" blends them.
What you'll see: a smooth, rippling red-and-blue pattern of concentric waves, with the x and y axes labeled from -2 to 2 (thanks to extent ) and the pixels blended into a continuous surface by bilinear interpolation.
Replace each ___ to display a random grid with the magma colormap and a colorbar.
Hint: the image method is imshow , and the figure method that adds the legend strip is colorbar .
You expected math orientation but got the image default. Add origin="lower" so row 0 sits at the bottom.
imshow needs a 2D array (or a 3D RGB array). A 1D list won't work — reshape it first with .reshape(rows, cols) .
Set aspect : use "equal" to keep pixels square, or "auto" to let the image fill the Axes.
Build a 5×5 heatmap with day labels on both axes and the numeric value written inside every cell.
Lesson complete — you can turn any matrix into a picture!
You displayed arrays with ax.imshow() , flipped the y-axis with origin , mapped pixels to data units with extent , and tuned shape with aspect and interpolation .
🚀 Up next: Tick Locators & Formatters — take full command of your axis labels.
Practice quiz
What does ax.imshow() draw?
- A line plot
- A 2D array as a grid of colored pixels
- A 3D mesh
- A pie chart
Answer: A 2D array as a grid of colored pixels. imshow renders a 2D array as colored pixels, one rectangle per cell.
What is the default value of the origin argument?
- 'lower'
- 'center'
- 'upper'
- 'auto'
Answer: 'upper'. The default origin='upper' places row 0 at the top, the image convention.
What does origin='lower' do?
- Puts row 0 at the bottom (math orientation)
- Puts row 0 at the top
- Flips left-right
- Reverses the colormap
Answer: Puts row 0 at the bottom (math orientation). origin='lower' places row 0 at the bottom, matching y-increases-upward math orientation.
What does extent=[xmin, xmax, ymin, ymax] do?
- Sets the colormap range
- Maps the array onto real data coordinates
- Sets the DPI
- Chooses interpolation
Answer: Maps the array onto real data coordinates. extent maps the array onto data coordinates so axes show units, not pixel indices.
Which aspect value lets the image stretch to fill the Axes?
- 'equal'
- 'auto'
- 'square'
- 'fit'
Answer: 'auto'. aspect='auto' lets the image stretch; 'equal' keeps pixels square.
Which interpolation keeps the pixels blocky (no smoothing)?
- 'bilinear'
- 'bicubic'
- 'nearest'
- 'gaussian'
Answer: 'nearest'. interpolation='nearest' keeps pixels blocky; 'bilinear' blends them.
When should you use pcolormesh instead of imshow?
- For a plain square-celled matrix
- For irregular or non-uniform grids with explicit edge coordinates
- For 1D data
- For faster rendering of images
Answer: For irregular or non-uniform grids with explicit edge coordinates. pcolormesh accepts explicit X/Y edges, handling irregular grids imshow cannot.
imshow returns a mappable. How do you add a colorbar to it?
- fig.colorbar(im, ax=ax)
- ax.legend(im)
- im.colorbar()
- plt.key(im)
Answer: fig.colorbar(im, ax=ax). Capture im = ax.imshow(...) and pass it to fig.colorbar(im, ax=ax).
A color photo passed to imshow is what shape?
- (height, width)
- (height, width, 3) for RGB channels
- (3, height)
- (width,)
Answer: (height, width, 3) for RGB channels. An RGB photo is a 3D array (height, width, 3); imshow displays it with no colormap.
If your image looks upside-down (you expected math orientation), you should...
- Set aspect='equal'
- Add origin='lower'
- Change the cmap
- Call colorbar()
Answer: Add origin='lower'. Add origin='lower' so row 0 sits at the bottom in math orientation.