Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to use Matplotlib to create subplots using the HBoxDivider and VBoxDivider classes. We will use a simple example to show how these classes can be used to arrange multiple subplots.

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/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) 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/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} matplotlib/importing_matplotlib -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} matplotlib/figures_axes -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} matplotlib/heatmaps -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} python/lists -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} python/tuples -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} python/importing_modules -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} python/numerical_computing -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} python/data_visualization -.-> lab-48663{{"`Matplotlib Subplot Arrangement Using HBoxDivider and VBoxDivider`"}} end

Import Required Libraries

We begin by importing the required libraries - matplotlib and numpy.

import matplotlib.pyplot as plt
import numpy as np

Create Data

We create two numpy arrays to be used as the data for our subplots.

arr1 = np.arange(20).reshape((4, 5))
arr2 = np.arange(20).reshape((5, 4))

Create Subplots Using HBoxDivider

We create two subplots side-by-side using the HBoxDivider class. We also adjust the axes' location so that they have equal heights while maintaining their aspect ratios.

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(arr1)
ax2.imshow(arr2)

pad = 0.5  ## pad in inches
divider = HBoxDivider(
    fig, 111,
    horizontal=[Size.AxesX(ax1), Size.Fixed(pad), Size.AxesX(ax2)],
    vertical=[Size.AxesY(ax1), Size.Scaled(1), Size.AxesY(ax2)])
ax1.set_axes_locator(divider.new_locator(0))
ax2.set_axes_locator(divider.new_locator(2))

plt.show()

Create Subplots Using VBoxDivider

We create two subplots one below the other using the VBoxDivider class. We adjust the axes' location so that they have equal widths while maintaining their aspect ratios.

fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.imshow(arr1)
ax2.imshow(arr2)

divider = VBoxDivider(
    fig, 111,
    horizontal=[Size.AxesX(ax1), Size.Scaled(1), Size.AxesX(ax2)],
    vertical=[Size.AxesY(ax1), Size.Fixed(pad), Size.AxesY(ax2)])

ax1.set_axes_locator(divider.new_locator(0))
ax2.set_axes_locator(divider.new_locator(2))

plt.show()

Summary

In this lab, we learned how to use the HBoxDivider and VBoxDivider classes in Matplotlib to create subplots. We saw how to adjust the axes' location so that they have equal heights or widths while maintaining their aspect ratios. These classes can be useful when we need to arrange multiple subplots in a single figure.

Other Python Tutorials you may like