Custom Matplotlib Grid Axes Creation

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create a custom grid of axes with Matplotlib using the mpl_toolkits.axes_grid1 module. We will create two examples: one with fixed axes sizes and paddings, and another with scalable axes sizes and fixed paddings.

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`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) 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`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} matplotlib/importing_matplotlib -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} matplotlib/figures_axes -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} python/booleans -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} python/lists -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} python/tuples -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} python/function_definition -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} python/importing_modules -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} python/using_packages -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} python/data_visualization -.-> lab-48930{{"`Custom Matplotlib Grid Axes Creation`"}} end

Importing Required Libraries

We will start by importing the required libraries: matplotlib.pyplot for visualization and mpl_toolkits.axes_grid1 for creating the custom grid of axes.

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

Defining Helper Function

We will define a helper function label_axes() that will be used to place a label at the center of an axes and remove the axis ticks.

def label_axes(ax, text):
    """Place a label at the center of an Axes, and remove the axis ticks."""
    ax.text(.5, .5, text, transform=ax.transAxes,
            horizontalalignment="center", verticalalignment="center")
    ax.tick_params(bottom=False, labelbottom=False,
                   left=False, labelleft=False)

Creating a Custom Grid of Axes with Fixed Sizes and Paddings

We will create a custom grid of axes with fixed sizes and paddings. We will use the Divider class to divide the axes rectangle into a grid with sizes specified by horiz * vert. We will then add four axes to the figure using the add_axes() method and specify the positions of each axes using the new_locator() method of the Divider class.

## Sizes are in inches.
horiz = [Size.Fixed(1.), Size.Fixed(.5), Size.Fixed(1.5), Size.Fixed(.5)]
vert = [Size.Fixed(1.5), Size.Fixed(.5), Size.Fixed(1.)]

rect = (0.1, 0.1, 0.8, 0.8)
fig = plt.figure(figsize=(6, 6))
fig.suptitle("Fixed axes sizes, fixed paddings")

div = Divider(fig, rect, horiz, vert, aspect=False)

## The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

plt.show()

Creating a Custom Grid of Axes with Scalable Sizes and Fixed Paddings

We will create another custom grid of axes with scalable sizes and fixed paddings. We will use the Size.Scaled() option to specify axes sizes that scale with the figure size. The remaining steps are similar to the previous example.

## Sizes are in inches.
horiz = [Size.Scaled(1.5), Size.Fixed(.5), Size.Scaled(1.), Size.Scaled(.5)]
vert = [Size.Scaled(1.), Size.Fixed(.5), Size.Scaled(1.5)]

rect = (0.1, 0.1, 0.8, 0.8)
fig = plt.figure(figsize=(6, 6))
fig.suptitle("Scalable axes sizes, fixed paddings")

div = Divider(fig, rect, horiz, vert, aspect=False)

## The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

plt.show()

Summary

In this lab, we learned how to create a custom grid of axes with Matplotlib using the mpl_toolkits.axes_grid1 module. We created two examples: one with fixed axes sizes and paddings, and another with scalable axes sizes and fixed paddings. We used the Divider class to divide the axes rectangle into a grid with sizes specified by horiz * vert and added axes to the figure using the add_axes() method and new_locator() method of the Divider class.

Other Python Tutorials you may like