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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} matplotlib/figures_axes -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} matplotlib/heatmaps -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} python/lists -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} python/tuples -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} python/importing_modules -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} python/using_packages -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} python/file_opening_closing -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} python/numerical_computing -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} python/data_visualization -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} python/build_in_functions -.-> lab-71149{{"`Image Plotting with Matplotlib`"}} end

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