Image Grid Alignment in Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

In this tutorial, we will learn how to use the ImageGrid function from the mpl_toolkits.axes_grid1 module in Matplotlib. We will create a 2x2 grid of images and align them using the ImageGrid function.

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/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`"]) 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/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-48932{{"`Image Grid Alignment in Matplotlib`"}} matplotlib/figures_axes -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} matplotlib/heatmaps -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} python/for_loops -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} python/lists -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} python/tuples -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} python/importing_modules -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} python/using_packages -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} python/numerical_computing -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} python/data_visualization -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} python/build_in_functions -.-> lab-48932{{"`Image Grid Alignment in Matplotlib`"}} end

Import necessary libraries and create image arrays

We begin by importing the necessary libraries and creating four 10x10 image arrays using the arange and reshape functions from the NumPy library.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import ImageGrid

im1 = np.arange(100).reshape((10, 10))
im2 = im1.T
im3 = np.flipud(im1)
im4 = np.fliplr(im2)

Create a figure and an ImageGrid object

Next, we create a figure object using the plt.figure function and pass in the figsize argument to set the size of the figure. We then create an ImageGrid object using the ImageGrid function and pass in the figure, 111 as the subplot argument, (2, 2) as the nrows_ncols argument to create a 2x2 grid of axes, and 0.1 as the axes_pad argument to set the padding between the axes.

fig = plt.figure(figsize=(4., 4.))
grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.1)

Iterate over the grid and plot the images

We then iterate over the grid object using the zip function to iterate over both the axes and the image arrays. We plot each image on its corresponding axis using the imshow function.

for ax, im in zip(grid, [im1, im2, im3, im4]):
    ax.imshow(im)

Show the plot

Finally, we show the plot using the plt.show function.

plt.show()

Summary

In this tutorial, we learned how to use the ImageGrid function from the mpl_toolkits.axes_grid1 module in Matplotlib to align multiple images in a grid. We created a 2x2 grid of images and plotted them using the imshow function.

Other Python Tutorials you may like