Matplotlib imshow() method
The imshow() function is a function in the Matplotlib library used to display images.
The imshow() function is commonly used to draw 2D grayscale or color images.
The imshow() function can be used to draw matrices, heatmaps, maps, etc.
The syntax of the imshow() method is as follows:
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, *, data=None, **kwargs)
Parameter Description:
X: Input data. It can be a 2D array, 3D array, PIL image object, matplotlib path object, etc.cmap: Color map. Used to control the colors corresponding to different values in the image. You can choose built-in color maps such asgray、hot、jetetc., or you can customize the color map.norm: Used to control how values are normalized. You can chooseNormalize、LogNormand other normalization methods.aspect: Controls the image aspect ratio. It can be set toautoor a number.interpolation: Interpolation method. Used to control the smoothness and level of detail of the image. You can choosenearest、bilinear、bicubicand other interpolation methods.alpha: Image transparency. The value range is 0~1.origin: The position of the coordinate axis origin. It can be set toupperorlower。extent: Controls the range of displayed data. It can be set to[xmin, xmax, ymin, ymax]。vmin、vmax: Controls the value range of the color map.filternorm 和 filterrad: An object used for image filtering. It can be set toNone、antigrain、freetype, etc.imlim: Used to specify the image display range.resample: Used to specify the image resampling method.url: Used to specify the image link.
The following are some examples of using the imshow() function.
Display the grayscale image
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Generate a 2D random array
img
=
np.
random
.
rand
(
10
,
10
)
Plot a grayscale image
plt.
imshow
(
img
,
cmap
=
'gray'
)
Displays an image
plt.
show
(
)
In the example above we generated a 10x10 random array and used the imshow() function to display it as a grayscale image.
We set the cmap parameter to gray, which means the image will be displayed using a grayscale color map.
The display result is as follows:

Display the color image
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Generate a random color image
img
=
np.
random
.
rand
(
10
,
10
,
3
)
Plot a color image
plt.
imshow
(
img
)
Displays an image
plt.
show
(
)
In the example above we generated a 10x10 random color image and used the imshow() function to display it.
Since a color image is a 3D array, there is no need to set the cmap parameter.
The display result is as follows:

Display a heatmap
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Generate a 2D random array
data
=
np.
random
.
rand
(
10
,
10
)
Draw a heatmap
plt.
imshow
(
data
,
cmap
=
'hot'
)
Displays an image
plt.
colorbar
(
)
plt.
show
(
)
In the example above we generated a 10x10 random array and used the imshow() function to display it as a heatmap.
We set the cmap parameter to hot, which means the image will be displayed using a heat color map.
In addition, we added a colorbar so we can see the relationship between data values and colors.
The display result is as follows:

Display a map
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
from
PIL
import
Image
Load the map image, download address: https://static.jyshare.com/images/demo/map.jpeg
img
=
Image.
open
(
'map.jpg'
)
Convert to an array
data
=
np.
array
(
img
)
Plot the map
plt.
imshow
(
data
)
Hide Axes
plt.
axis
(
'off'
)
Displays an image
plt.
show
(
)
In the example above we loaded a map image and converted it into an array.
Then we used the imshow() function to display it and the axis('off') function to hide the axes, so the map can be viewed better.
The display result is as follows:

Display a matrix
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Generate a random matrix
data
=
np.
random
.
rand
(
10
,
10
)
Plot the matrix
plt.
imshow
(
data
)
Displays an image
plt.
show
(
)
In the example above we generated a random matrix and used the imshow() function to display it as an image.
Since a matrix is also a 2D array, it can be displayed using the imshow() function.
The display result is as follows:

More examples
The following creates a 4x4 2D numpy array and displays it in three different ways using imshow.
- The first shows the grayscale color mapping without any color blending.
- The second shows the image using the viridis color map, also without any color blending.
- The third shows the image using the viridis color map and uses bicubic interpolation for color blending.
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
n
=
4
Create an n x n 2D numpy array
a
=
np.
reshape
(
np.
linspace
(
0
,
1
,
n**
2
)
,
(
n
,
n
)
)
plt.
figure
(
figsize
=
(
12
,
4.5
)
)
The first image shows the grayscale color mapping without any color blending
plt.
subplot
(
131
)
plt.
imshow
(
a
,
cmap
=
'gray'
,
interpolation
=
'nearest'
)
plt.
xticks
(
range
(
n
)
)
plt.
yticks
(
range
(
n
)
)
Grayscale mapping, no blending
plt.
title
(
'Gray color map, no blending'
,
y
=
1.02
,
fontsize
=
12
)
The second image shows the image using the viridis color map, also without any color blending
plt.
subplot
(
132
)
plt.
imshow
(
a
,
cmap
=
'viridis'
,
interpolation
=
'nearest'
)
plt.
yticks
(
[
]
)
plt.
xticks
(
range
(
n
)
)
Viridis mapping, no blending
plt.
title
(
'Viridis color map, no blending'
,
y
=
1.02
,
fontsize
=
12
)
The third image shows the image using the viridis color map and uses bicubic interpolation for color blending
plt.
subplot
(
133
)
plt.
imshow
(
a
,
cmap
=
'viridis'
,
interpolation
=
'bicubic'
)
plt.
yticks
(
[
]
)
plt.
xticks
(
range
(
n
)
)
Viridis mapping, bicubic blending
plt.
title
(
'Viridis color map, bicubic blending'
,
y
=
1.02
,
fontsize
=
12
)
plt.
show
(
)
The display result is as follows:
