Image Plotting with Matplotlib

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to plot and manipulate images using the Matplotlib library in Python. You will learn how to import image data into NumPy arrays, plot numpy arrays as images, apply pseudocolor schemes, add color scale references, examine specific data ranges, and explore different interpolation schemes.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Importing Image Data

To begin, we need to import the necessary libraries and load the image data into a NumPy array. In our case, we will use the PIL library to load the image, and then convert it into a NumPy array.

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

img = np.asarray(Image.open('./stinkbug.png'))

Plotting Images

Now that we have the image data in a NumPy array, we can plot it using the imshow function from matplotlib.pyplot. This function takes the image array as input and displays it as an image plot.

plt.imshow(img)

Applying Pseudocolor Schemes

Pseudocolor schemes can be used to enhance contrast and visualize data more easily. If the image is grayscale, we can apply pseudocolor schemes by specifying different colormaps. We can do this by using the cmap parameter in the imshow function.

lum_img = img[:, :, 0]
plt.imshow(lum_img, cmap="hot")

Adding Color Scale Reference

To provide a reference for the color scale, we can add a color bar to the plot. This can be done using the colorbar function from matplotlib.pyplot.

imgplot = plt.imshow(lum_img)
plt.colorbar()

Examining Specific Data Ranges

Sometimes, it might be necessary to examine specific data ranges in an image. We can do this by adjusting the colormap limits using the clim parameter in the imshow function. This allows us to focus on specific regions of the image while sacrificing detail in other regions.

min_value, max_value = 100, 200
plt.imshow(img, clim=(min_value, max_value))

Array Interpolation Schemes

When resizing an image, it is necessary to interpolate the pixel values to fill the missing space. Different interpolation schemes can be used to determine the value of a pixel based on its surrounding pixels. Matplotlib provides different interpolation options, such as "nearest", "bilinear", and "bicubic".

plt.imshow(img, interpolation="bilinear")

Summary

In this lab, you have learned how to plot and manipulate images using Matplotlib. You have learned how to import image data into NumPy arrays, plot numpy arrays as images, apply pseudocolor schemes, add color scale references, examine specific data ranges, and explore different interpolation schemes. These skills will be useful for visualizing and analyzing images in various applications.

Other Matplotlib Tutorials you may like