O'Reilly logo

Matplotlib for Python Developers by Allen Yu, Claire Chung, Aldrin Yim

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Image plotting

In analyzing images, the first step is to convert colors into numerical values. Matplotlib provides APIs to read and show an image matrix of RGB values. 

The following is a quick code example of reading an image into a NumPy array with plt.imread('image_path'), and we show it with plt.imshow(image_ndarray). Make sure that the Pillow package is installed so that more image types other than PNG can be handled:

import matplotlib.pyplot as plt# Source image downloaded under CC0 license: Free for personal and commercial use. No attribution required.# Source image address: https://pixabay.com/en/rose-pink-blossom-bloom-flowers-693155/img = plt.imread('ch04.img/mpldev_ch04_rose.jpg')plt.imshow(img)

Here is the original image displayed ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required