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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") 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 matplotlib/importing_matplotlib -.-> lab-48931{{"`Matplotlib Axes Divider`"}} matplotlib/figures_axes -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/booleans -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/for_loops -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/list_comprehensions -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/lists -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/tuples -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/importing_modules -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/using_packages -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/data_visualization -.-> lab-48931{{"`Matplotlib Axes Divider`"}} python/build_in_functions -.-> lab-48931{{"`Matplotlib Axes Divider`"}} end

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