HOWTO · Matplotlib

How to Display an Image in Grayscale in Matplotlib

This article introduces how to display a grayscale image in Matplotlib.

On this page

To display a grayscale image in Matplotlib, we use the matplotlib.pyplot.imshow() with parameters cmap set to 'gray', vmin set to 0 and vmax set to 255.By default, the value of cmap, vmin and vmax is set to None.

matplotlib.pyplot.imshow() to Display an Image in Grayscale in Matplotlib

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)

Examples: Matplotlib Display Image in Grayscale

import matplotlib.pyplot as plt
import matplotlib.image as img

image = img.imread("lena.jpg")

plt.imshow(image[:, :, 1], cmap="gray", vmin=0, vmax=255, interpolation="none")
plt.show()

Output:

Matplotlib display Image in Grayscale

This method reads the image lena.jpg, which is an RGB image using the imread() function from the matplotlib.image module. To display the image as grayscale, we only need one color channel. So for the next step, only take a single color channel and display the image using the plt.imshow() method with cmap set to 'gray', vmin set to 0, and vmax set to 255.

Finally, we use the show() method to show a window displaying the image in grayscale. To run the script above, we must have lena.jpg in our current working directory.

Taking only one color channel is not an appropriate way to convert RGB images to grayscale images because we have specific algorithms to make the conversion more efficient. These algorithms are implemented in almost all popular image processing libraries available. Below, we demonstrate an example using the PIL.

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

image = Image.open("lena.jpg")
gray_image = image.convert("L")
gray_image_array = np.asarray(gray_image)
plt.imshow(gray_image_array, cmap="gray", vmin=0, vmax=255)
plt.show()

Output:

Matplotlib display Image in Grayscale using Image Module

Here, we read the image lena.jpg in our current working directory using the open() function and then convert the image to grayscale using the convert() method with 'L' as an argument. After that, we convert the grayscale image into a NumPy array and display the image using the imshow() method.

Display a Two-Dimensional Array in Grayscale in Matplotlib

The examples above read an image file from disk. imshow() works the same way with a two-dimensional array that the program creates itself: build the array with NumPy, then pass it to imshow() with cmap set to "gray", and set vmin and vmax to the array’s own minimum and maximum values so the full range of the data maps from black to white.

import numpy as np
import matplotlib.pyplot as plt

# Build a two-dimensional array of grayscale values in code
x = np.linspace(0, 255, 16)
y = np.linspace(0, 255, 12)
data = (x[np.newaxis, :] + y[:, np.newaxis]) / 2

plt.imshow(data, cmap="gray", vmin=data.min(), vmax=data.max())
plt.colorbar(label="Pixel value")
plt.title("Two-dimensional array displayed in grayscale")
plt.show()

Output:

A two-dimensional NumPy array displayed in grayscale with imshow: the brightness increases smoothly from black in the top-left corner (value 0) to white in the bottom-right corner (value 255), and a colorbar shows the pixel values.

The array holds values from 0 to 255, so the darkest cell renders black, the brightest renders white, and every value in between renders as a proportional shade of gray.