Align Images Using Matplotlib's ImageGrid

PythonPythonBeginner
Practice Now

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

Introduction

In this tutorial, we will learn how to use ImageGrid from mpl_toolkits.axes_grid1 in Matplotlib to align multiple images of different sizes.

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 python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") 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/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} matplotlib/importing_matplotlib -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} matplotlib/figures_axes -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} matplotlib/heatmaps -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} python/for_loops -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} python/lists -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} python/tuples -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} python/importing_modules -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} python/using_packages -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} python/data_visualization -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} python/build_in_functions -.-> lab-48933{{"`Align Images Using Matplotlib's ImageGrid`"}} end

Import the necessary libraries

First, we need to import the necessary libraries including Matplotlib, cbook and ImageGrid.

import matplotlib.pyplot as plt
from matplotlib import cbook
from mpl_toolkits.axes_grid1 import ImageGrid

Create a Figure and ImageGrid

Next, we create a figure and ImageGrid with nrows_ncols parameter to define the number of rows and columns of the grid.

fig = plt.figure(figsize=(5.5, 3.5))
grid = ImageGrid(fig, 111,  ## similar to subplot(111)
                 nrows_ncols=(1, 3),
                 axes_pad=0.1,
                 label_mode="L")

Load the image data

We will use an example image data called bivariate_normal.npy from cbook to demonstrate the ImageGrid. We load the image data using get_sample_data function from cbook.

Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
im1 = Z
im2 = Z[:, :10]
im3 = Z[:, 10:]
vmin, vmax = Z.min(), Z.max()

Display images in the ImageGrid

Finally, we display the images in the ImageGrid using imshow function and zip function to iterate through the axes in the grid.

for ax, im in zip(grid, [im1, im2, im3]):
    ax.imshow(im, origin="lower", vmin=vmin, vmax=vmax)

plt.show()

Summary

In this tutorial, we have learned how to use ImageGrid in Matplotlib to align multiple images of different sizes. We first import the necessary libraries, then create a figure and ImageGrid, load the image data, and finally display the images in the ImageGrid.

Other Python Tutorials you may like