Skip to content
Home » matplotlib Part 16 – The Subplot2grid Layout Manager

matplotlib Part 16 – The Subplot2grid Layout Manager

Spread the love

The Subplot2grid Layout Manager

Most of the time we’ve been creating figures and axes using the plt.subplots method. Today let’s have a look at a layout manager that can help us with it a little.

The subplot2grid manager enables us to create Axes instances that span multiple rows and/or columns. To use the manager, we have to call the plt.subplot2grid method with some arguments. In particular, there are two mandatory arguments:

  • the shape of the axis, which is a tuple with two values: the number of rows and the number of columns
  • the starting position within the grid, which is a tuple two with the row and column number where the Axes instance should start

You can also use the optional rowspan and colspan arguments if you want the Axes instance to span several rows or columns respectively.

Let’s demonstrate the subplot2grid method on an example. Let’s create a 4 x 4 grid (four rows, four columns) and then place some Axes instances in it. In the example below I also added some text annotations and removed the ticks for clarity’s sake.

In [19]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

# The first Axes instance should be in row 0, column 0.
ax0 = plt.subplot2grid((4, 4), (0, 0))
ax0.text(.5, .5, 'ax0')
ax0.xaxis.set_major_locator(mpl.ticker.NullLocator())
ax0.yaxis.set_major_locator(mpl.ticker.NullLocator())

# The second Axes instance should start in row 0, column 1 and span two columns.
ax1 = plt.subplot2grid((4, 4), (0, 1), colspan=2)
ax1.text(.5, .5, 'ax1')
ax1.xaxis.set_major_locator(mpl.ticker.NullLocator())
ax1.yaxis.set_major_locator(mpl.ticker.NullLocator())

# The third Axes instance should start in row 1, column 0 and span three rows.
ax2 = plt.subplot2grid((4, 4), (1, 0), rowspan=3)
ax2.text(.5, .5, 'ax2')
ax2.xaxis.set_major_locator(mpl.ticker.NullLocator())
ax2.yaxis.set_major_locator(mpl.ticker.NullLocator())

# The fourth Axes instance should start in row 1, column 1 and span three rows and two columns.
ax3 = plt.subplot2grid((4, 4), (1, 1), rowspan=3, colspan=2)
ax3.text(.5, .5, 'ax3')
ax3.xaxis.set_major_locator(mpl.ticker.NullLocator())
ax3.yaxis.set_major_locator(mpl.ticker.NullLocator())

# The fifth Axes instance should start in row 0, column 3 and span all four rows.
ax4 = plt.subplot2grid((4, 4), (0, 3), rowspan=4)
ax4.text(.5, .5, 'ax4')
ax4.xaxis.set_major_locator(mpl.ticker.NullLocator())
ax4.yaxis.set_major_locator(mpl.ticker.NullLocator())

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply