HOWTO · Matplotlib
How to Display an Image With Matplotlib Python
Learn how to load and display a local image with Matplotlib's imshow(), display a PIL image, customize its appearance, and save the figure.
On this page
Use matplotlib.pyplot.imread() to load an image, imshow() to place its pixels in a Matplotlib axes, and show() to display the figure in a script. The path passed to imread() must identify an image available to the Python process. In notebooks, the final figure is often displayed automatically; in a regular script, call plt.show().
Load and Display an Image with Matplotlib
This self-contained example creates a small sample image, reads it with imread(), and displays it. For your file, replace the sample-image setup and image_path assignment with the path to an image accessible to the Python process. Put the image beside the script and use its filename, or provide an absolute path; relative paths are resolved from the process’s current working directory.
from pathlib import Path
from tempfile import TemporaryDirectory
import matplotlib.pyplot as plt
import numpy as np
with TemporaryDirectory() as directory:
image_path = Path(directory) / "sample.png"
pixels = np.zeros((3, 3, 3), dtype=float)
pixels[:, :, 0] = 1.0
plt.imsave(image_path, pixels)
image = plt.imread(image_path)
plt.imshow(image)
plt.axis("off")
plt.show()
print("Image loaded and displayed.")
Image loaded and displayed.
imread() loads the file into an array, and imshow() is a function in matplotlib.pyplot that displays that image data in the current axes. axis("off") hides the axes and their ticks. The generated sample is only a fixture for this runnable example; when using your own image, keep that file available at the chosen path and read it directly.
Display a PIL Image with Matplotlib
If an image is already represented by a PIL image object, pass that object to imshow() instead of reading it with plt.imread(). This is useful when another part of your program has already opened or processed the image with Pillow. Image.show() is a separate PIL display route; use plt.show() to display the Matplotlib figure. The two display calls use different rendering paths, so use the Matplotlib route when you need axes, figure sizing, or figure export.
from pathlib import Path
from tempfile import TemporaryDirectory
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
with TemporaryDirectory() as directory:
image_path = Path(directory) / "sample.png"
plt.imsave(image_path, np.full((3, 3, 3), 0.4))
with Image.open(image_path) as image:
plt.imshow(image)
plt.axis("off")
plt.show()
Replace the temporary sample path with your own image path to open a local file. This route needs Pillow, which provides PIL.Image.
Customize Image Display
For a grayscale view of RGB data, average its three color channels and select a grayscale colormap. The mean(axis=2) operation below assumes the array’s third axis contains the red, green, and blue channels; it is not appropriate for every image shape or color model. Interpolation changes how Matplotlib renders pixels on screen, not the source image’s detail.
import matplotlib.pyplot as plt
import numpy as np
image = np.array(
[
[[1.0, 0.2, 0.1], [0.2, 0.8, 0.3]],
[[0.1, 0.3, 1.0], [0.8, 0.8, 0.8]],
]
)
plt.imshow(image.mean(axis=2), cmap="gray", interpolation="bicubic")
plt.axis("off")
plt.show()
cmap="gray" maps the averaged values to shades of gray. Choose another supported colormap or interpolation setting when it better suits the image and the way it will be viewed. To preserve the color image, pass the original array to imshow() instead of the averaged array. The display options affect the rendered view; they do not rewrite the loaded pixel data.
Save the Displayed Figure
Use savefig() to write the Matplotlib figure to an image file. It saves the figure, including its axes and layout, rather than changing the original image. Save before calling show() so the figure is still available to the save operation across display backends.
import matplotlib.pyplot as plt
import numpy as np
image = np.full((3, 3, 3), 0.4)
plt.imshow(image)
plt.axis("off")
plt.savefig("displayed-image.png", bbox_inches="tight", pad_inches=0)
plt.show()
The path displayed-image.png is written relative to the current working directory. Choose a different filename or path to control where the figure is saved. For a tight crop, bbox_inches="tight" trims surrounding whitespace and pad_inches=0 removes the extra padding. Set the figure size before plotting if the exported dimensions matter; the saved file is a rendering of the current Matplotlib figure.