Signup/Sign In

Matplotlib subplot2grid() function

In this tutorial, we will cover subplot2grid() function in state-based interface i.e Pyplot in the Matplotlib library

This function is used to provide additional flexibility in creating axes object at a particular specified location inside a grid.

  • For the spanning of the axes object across multiple rows or columns, we will use this method.

  • subplot2grid() function is also termed as a sub-figure layout manager.

  • In short terms, we will use this function to create multiple charts within the same figure.

Matplotlib subplot2grid() Function

The subplot2grid() function or the matplotlib.pyplot.subplot2grid function can be used easily to create multiple charts within same figure, here is the syntax for it:

matplotlib.pyplot.subplot2grid(shape, location, rowspan, colspan)

Matplotlib subplot2grid() Parameters:

Let us discuss the parameters used by this function:

  • shape

    The shape parameter is used to indicate the shape of the grid to be plotted inside the graph. It is a mandatory argument for this method.

    It is generally passed as a list or tuple of two numbers that are mainly responsible for the layout of the grid and the first number indicates the number of rows while the second number indicates the number of columns.

  • location

    This parameter is also a mandatory parameter taken by this function. This method is similar to the shape argument and is also generally passed in as a list or tuple of two numbers. It is used for specifying the row and column number where the sub-plot will be placed. One thing to note is that the indexes always start from 0.

    So (0, 0) is the cell in the first row and the first column of the grid.

  • rowspan

    After setting the grid layout and the starting index using the location(loc) parameter, one can also expand the selection to take up more rows with this argument if needed. This is an optional parameter having 1 as default value.

  • colspan

    This parameter is similar to rowspan parameter and it is used in order to expand the selection to take up more columns. This is also an optional parameter having 1 as the default value.

Example:

Let us draw a 3x3 grid of the figure object that is filled with axes objects of varying sizes in a row and column spans. The code snippet for the same is given below:

import matplotlib.pyplot as plt

def annotate_axes(fig):
    for i, ax in enumerate(fig.axes):
        ax.text(0.5, 0.5, "block%d" % (i+1), va="center", ha="center")
        ax.tick_params(labelbottom=False, labelleft=False)


fig = plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))

annotate_axes(fig)

plt.show()

The output of the above code snippet is as follows:

Matplotlib subplot2grid() Function example

Summary:

So now you know how to make multiple axes objects in a single figure using the Matplotlib subplot2grid() function which is easy to use as we can see in the code example above.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.