If I understand your situation correctly, pyplot.imshow() will know nothing about the individual image dimensions, as you have it right now. It will consider your matrix as pixel values for a single image, of dimensions 20 pixels by 400 pixels, because that is the shape of your matrix. For instance:

import numpy as np
import matplotlib.pyplot as plt

''' Create a matrix of random values, of shape (20,400)
I used random integer values here between 0 and 255 
but you can do the same for decimal pixel intensities '''

mat = np.random.randint(0,255,400*20).reshape(20,400)
# Call imshow:
plt.imshow(mat, cmap='gray')
plt.show()

Gives you this image:

Since your 20x20 images are essentially stored in the second dimension of the matrix, you can show an individual image, in 20x20 format (you explicitly have to reshape it, though), as such:

plt.imshow(mat[0,:].reshape(20,20), cmap='gray')
plt.show()

This returns the 1st image:

For the second image, use mat[1,:].reshape(20,20), etc...

[EDIT]: To see how imshow() plots your images row by row, consider the following matrix in which pixel intensities are steadily decreasing:

example_mat = np.linspace(1,0, 25).reshape(5,5)

>>> example_mat
array([[ 1.        ,  0.95833333,  0.91666667,  0.875     ,  0.83333333],
       [ 0.79166667,  0.75      ,  0.70833333,  0.66666667,  0.625     ],
       [ 0.58333333,  0.54166667,  0.5       ,  0.45833333,  0.41666667],
       [ 0.375     ,  0.33333333,  0.29166667,  0.25      ,  0.20833333],
       [ 0.16666667,  0.125     ,  0.08333333,  0.04166667,  0.        ]])

If you call imshow() on this matrix, you get this image:

As you can see, the first "row" of your matrix (example_mat[0,:]) is the first (i.e. top) "row" of your image.

Answer from sacuL on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › matplotlib-pyplot-imshow-in-python
matplotlib.pyplot.imshow() in Python - GeeksforGeeks
July 12, 2025 - imshow() visualizes the array as an image, using the 'viridis' colormap and 'nearest' interpolation. plt.colorbar() adds a colorbar to the side of the image, showing the value-to-color mapping.
Top answer
1 of 1
5

If I understand your situation correctly, pyplot.imshow() will know nothing about the individual image dimensions, as you have it right now. It will consider your matrix as pixel values for a single image, of dimensions 20 pixels by 400 pixels, because that is the shape of your matrix. For instance:

import numpy as np
import matplotlib.pyplot as plt

''' Create a matrix of random values, of shape (20,400)
I used random integer values here between 0 and 255 
but you can do the same for decimal pixel intensities '''

mat = np.random.randint(0,255,400*20).reshape(20,400)
# Call imshow:
plt.imshow(mat, cmap='gray')
plt.show()

Gives you this image:

Since your 20x20 images are essentially stored in the second dimension of the matrix, you can show an individual image, in 20x20 format (you explicitly have to reshape it, though), as such:

plt.imshow(mat[0,:].reshape(20,20), cmap='gray')
plt.show()

This returns the 1st image:

For the second image, use mat[1,:].reshape(20,20), etc...

[EDIT]: To see how imshow() plots your images row by row, consider the following matrix in which pixel intensities are steadily decreasing:

example_mat = np.linspace(1,0, 25).reshape(5,5)

>>> example_mat
array([[ 1.        ,  0.95833333,  0.91666667,  0.875     ,  0.83333333],
       [ 0.79166667,  0.75      ,  0.70833333,  0.66666667,  0.625     ],
       [ 0.58333333,  0.54166667,  0.5       ,  0.45833333,  0.41666667],
       [ 0.375     ,  0.33333333,  0.29166667,  0.25      ,  0.20833333],
       [ 0.16666667,  0.125     ,  0.08333333,  0.04166667,  0.        ]])

If you call imshow() on this matrix, you get this image:

As you can see, the first "row" of your matrix (example_mat[0,:]) is the first (i.e. top) "row" of your image.

🌐
Medium
medium.com › @heyamit10 › guide-to-using-matplotlib-imshow-18aa42a26656
Guide to Using matplotlib imshow
May 18, 2025 - You can modify the aspect ratio using the aspect parameter in the imshow() function. Set it to 'auto' to allow Matplotlib to adjust it automatically, or use a float value to specify it yourself. Can I combine multiple images? Absolutely! You can use subplots to create a grid of images. Here’s a small example: plt.subplot(1, 2, 1) plt.imshow(data, cmap='plasma') plt.title('First Image') plt.subplot(1, 2, 2) plt.imshow(img) plt.title('Second Image') plt.tight_layout() plt.show()
🌐
University at Buffalo
math.buffalo.edu › ~badzioch › MTH337 › PT › PT-image_processing › PT-image_processing.html
Image processing — MTH 337
The color of each square is determined by the value of the corresponding array element and the color map used by imshow(). import matplotlib.pyplot as plt import numpy as np n = 4 # create an nxn numpy array a = np.reshape(np.linspace(0,1,n**2), (n,n)) plt.figure(figsize=(12,4.5)) #use imshow to plot the array plt.subplot(131) plt.imshow(a, #numpy array generating the image cmap = 'gray', #color map used to specify colors interpolation='nearest' #algorithm used to blend square colors; with 'nearest' colors will not be blended ) plt.xticks(range(n)) plt.yticks(range(n)) plt.title('Gray color ma
🌐
Matplotlib
matplotlib.org › 2.1.2 › api › _as_gen › matplotlib.pyplot.imshow.html
matplotlib.pyplot.imshow — Matplotlib 2.1.2 documentation
matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, hold=None, data=None, **kwargs)¶
🌐
Matplotlib
matplotlib.org › 3.2.1 › api › _as_gen › matplotlib.pyplot.imshow.html
matplotlib.pyplot.imshow — Matplotlib 3.2.1 documentation
matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=<deprecated parameter>, filternorm=1, filterrad=4.0, imlim=<deprecated parameter>, resample=None, url=None, \*, data=None, \*\*kwargs)[source]¶
Find elsewhere
🌐
Matplotlib
matplotlib.org › 3.1.3 › api › _as_gen › matplotlib.pyplot.imshow.html
matplotlib.pyplot.imshow — Matplotlib 3.1.3 documentation
February 9, 2020 - matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=<deprecated parameter>, filternorm=1, filterrad=4.0, imlim=<deprecated parameter>, resample=None, url=None, *, data=None, **kwargs)[source]¶
🌐
Ufkapano
ufkapano.github.io › scicomppy › week09 › plt_imshow.html
Matplotlib - imshow()
# heatmap1.py import numpy as np import matplotlib.pyplot as plt data = np.random.random((8, 8)) #data = np.arange(100).reshape((10,10)) # data will be scaled to [0,1] plt.imshow(data, cmap='hot', interpolation='nearest') # pixelated # 0.0 black, 0.37 red, 0.75 yellow, 1.0 white #plt.imshow(data, cmap='cool', interpolation='bilinear') # blurry plt.colorbar() #plt.colorbar(label='temperature') plt.show()
🌐
Matplotlib
matplotlib.org › stable › gallery › images_contours_and_fields › image_demo.html
Many ways to plot images — Matplotlib 3.11.2 documentation
delta = 0.025 x = y = np.arange(-3.0, 3.0, delta) X, Y = np.meshgrid(x, y) Z1 = np.exp(-X**2 - Y**2) Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2 fig, ax = plt.subplots() im = ax.imshow(Z, interpolation='bilinear', cmap="RdYlBu", origin='lower', extent=[-3, 3, -3, 3], vmax=abs(Z).max(), vmin=-abs(Z).max()) plt.show()
🌐
Matplotlib
matplotlib.org › stable › tutorials › images.html
Image tutorial — Matplotlib 3.11.2 documentation
So, you have your data in a numpy array (either by importing it, or by generating it). Let's render it. In Matplotlib, this is performed using the imshow() function. Here we'll grab the plot object. This object gives you an easy way to manipulate the plot from the prompt. imgplot = plt.imshow(img) You can also plot any numpy array.
🌐
Matplotlib
matplotlib.org › 2.1.0 › api › _as_gen › matplotlib.pyplot.imshow.html
matplotlib.pyplot.imshow — Matplotlib 2.1.0 documentation
matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, hold=None, data=None, **kwargs)¶
🌐
Matplotlib
matplotlib.org › 3.1.1 › api › _as_gen › matplotlib.pyplot.imshow.html
matplotlib.pyplot.imshow — Matplotlib 3.1.2 documentation
matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=<deprecated parameter>, filternorm=1, filterrad=4.0, imlim=<deprecated parameter>, resample=None, url=None, *, data=None, **kwargs)[source]¶
🌐
YouTube
youtube.com › amir charkhi
Matplotlib Tutorial - Part 12: Show Images Using IMSHOW - YouTube
In this video, I will be showing you how to make your first imshow code, customize the visualization, and also how to make sure you prepare for a strong pres...
Published: September 6, 2022
Views: 11K
🌐
Python Pool
pythonpool.com › home › matplotlib › matplotlib imshow(): arrays, colormaps, and image display
Matplotlib imshow(): Arrays, Colormaps, and Image Display
July 13, 2026 - Display images, heatmaps, masks, and arrays with imshow() while controlling color scale, coordinates, interpolation, and aspect.
🌐
Chrisholdgraf
chrisholdgraf.com › matplotlib › api › _as_gen › matplotlib.pyplot.imshow.html
matplotlib.pyplot.imshow — Matplotlib 2.0.0b1.post7580.dev0+ge487118 documentation
matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, hold=None, data=None, **kwargs)¶
🌐
Finxter
blog.finxter.com › home › learn python blog › matplotlib imshow — a helpful illustrated guide
Matplotlib Imshow - A Helpful Illustrated Guide - Be on the Right Side of Change
March 3, 2020 - There are many different colormaps you can apply to your images. Simply pass the name to the cmap keyword argument in plt.imshow() and you’re good to go.
🌐
GeeksforGeeks
geeksforgeeks.org › python › matplotlib-axes-axes-imshow-in-python
Matplotlib.axes.Axes.imshow() in Python - GeeksforGeeks
July 12, 2025 - The Axes.imshow() function in axes module of matplotlib library is also used to display an image or data on a 2D regular raster.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-change-imshow-aspect-ratio-in-matplotlib
How to change imshow aspect ratio in Matplotlib? - GeeksforGeeks
July 23, 2025 - import matplotlib.pyplot as plt import cv2 # reading image from directory im = cv2.imread("C://Users/User/Downloads/chess5.png") # plotting a figure for showing all # images in a single plot fig = plt.figure(figsize=(4, 4)) # plotting each matplot image with # different aspect ratio parameter values # in a separate subplot ax1 = fig.add_subplot(2, 2, 1) ax1.set_xlabel('Original') # plot the initial image as the first image plt.imshow(im) ax2 = fig.add_subplot(2, 2, 2) ax2.set_xlabel('Aspect Ratio : Auto') # plot the image with "auto" aspect ratio # as the second image plt.imshow(im, aspect='au