Table of Contents
To display an image in grayscale using Matplotlib, you can use the “cmap” parameter in the imshow() function and set it equal to “gray”. This will convert the image to a grayscale format and display it accordingly. As an example, you can use the following code:
“`
import matplotlib.pyplot as plt
# load the image
img = plt.imread(“image.jpg”)
# display the image in grayscale
plt.imshow(img, cmap=”gray”)
# show the plot
plt.show()
“`
This will result in the image being displayed in grayscale. The “cmap” parameter can also be used to display images in other color maps, such as “hot” or “cool”. It is a useful tool for visualizing images in different color scales.
Display an Image as Grayscale in Matplotlib (With Example)
You can use the cmap argument in Matplotlib to easily display images on a .
The following example shows how to use this argument in practice.
Example: Display Image as Grayscale in Matplotlib
Suppose I have the following image called shapes.JPG that I’d like to display in Matplotlib:

I can use the following syntax to display this image using the original colors:
import numpy as np import matplotlib.pyplotas plt from PIL import Image image=Image.open('shapes.JPG') plt.imshow(image) plt.show()

Notice that this image perfectly matches the image I had on file.
In order to display the image on a grayscale, I must use the cmap=’gray’ argument as follows:
import numpy as np import matplotlib.pyplotas plt from PIL import Image #open image image=Image.open('shapes.JPG') #convert image to black and white pixels gray_image=image.convert('L') #convert image to NumPy array gray_image_array=np.asarray(gray_image) #display image on grayscale plt.imshow(gray_image_array, cmap='gray') plt.show()

The image has now been converted to a grayscale.
Note: The ‘L’ argument converts the image to black and white pixels. Without first using this line of code, the image will not display as a grayscale.
Additional Resources
The following tutorials explain how to perform other common tasks in Matplotlib:
Cite this article
stats writer (2024). How can I display an image as grayscale in Matplotlib, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-display-an-image-as-grayscale-in-matplotlib-and-can-you-provide-an-example/
stats writer. "How can I display an image as grayscale in Matplotlib, and can you provide an example?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-display-an-image-as-grayscale-in-matplotlib-and-can-you-provide-an-example/.
stats writer. "How can I display an image as grayscale in Matplotlib, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-display-an-image-as-grayscale-in-matplotlib-and-can-you-provide-an-example/.
stats writer (2024) 'How can I display an image as grayscale in Matplotlib, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-display-an-image-as-grayscale-in-matplotlib-and-can-you-provide-an-example/.
[1] stats writer, "How can I display an image as grayscale in Matplotlib, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I display an image as grayscale in Matplotlib, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
