Matplotlib Axes Divider

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to use the Matplotlib Axes Divider to create custom layouts for subplots in a figure.

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.

Import necessary libraries

We will start by importing the necessary libraries for this lab: matplotlib.pyplot and mpl_toolkits.axes_grid1.

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import Divider
import mpl_toolkits.axes_grid1.axes_size as Size

Set up the figure and axes

We will create a figure object and set up four axes objects using the fig.add_axes method.

fig = plt.figure(figsize=(5.5, 4))
rect = (0.1, 0.1, 0.8, 0.8)
ax = [fig.add_axes(rect, label="%d" % i) for i in range(4)]

Set up the axes divider

We will set up the axes divider using the Divider class and AxesX and AxesY classes from the mpl_toolkits.axes_grid1.axes_size module. Then, we will use the new_locator method to set the position of each axis.

horiz = [Size.AxesX(ax[0]), Size.Fixed(.5), Size.AxesX(ax[1])]
vert = [Size.AxesY(ax[0]), Size.Fixed(.5), Size.AxesY(ax[2])]
divider = Divider(fig, rect, horiz, vert, aspect=False)

ax[0].set_axes_locator(divider.new_locator(nx=0, ny=0))
ax[1].set_axes_locator(divider.new_locator(nx=2, ny=0))
ax[2].set_axes_locator(divider.new_locator(nx=0, ny=2))
ax[3].set_axes_locator(divider.new_locator(nx=2, ny=2))

Customize the axes limits and appearance

We will customize the limits and appearance of each axis using the set_xlim, set_ylim, and tick_params methods.

ax[0].set_xlim(0, 2)
ax[1].set_xlim(0, 1)
ax[0].set_ylim(0, 1)
ax[2].set_ylim(0, 2)
for ax1 in ax:
    ax1.tick_params(labelbottom=False, labelleft=False)

Display the plot

Finally, we will display the plot using the show method.

plt.show()

Summary

In this lab, we learned how to use the Matplotlib Axes Divider to create custom layouts for subplots in a figure. We created a figure object and set up four axes objects, then used the axes divider to position the axes in a grid. We customized the limits and appearance of each axis and displayed the plot using the show method.

Other Python Tutorials you may like