A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/matplotlib-pyplot-subplot2grid-in-python/ below:

Matplotlib.pyplot.subplot2grid() in python - GeeksforGeeks

Matplotlib.pyplot.subplot2grid() in python

Last Updated : 12 Jul, 2025

Matplotlib

is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.

The Matplotlib.pyplot.subplot2grid() function give additional flexibility in creating axes object at a specified location inside a grid. It also helps in spanning the axes object across multiple rows or columns. In simpler words, this function is used to create multiple charts within the same figure. It is a sub-figure layout manager.

Syntax : Plt.subplot2grid(shape, location, rowspan, colspan) Parameters :
Example 1: Python3 1==
import matplotlib.pyplot as plt


fig = plt.figure()

axes1 = plt.subplot2grid((4, 4), (0, 0),
                         colspan = 4)

axes2 = plt.subplot2grid((4, 4), (1, 0),
                         colspan = 3)

axes3 = plt.subplot2grid((4, 4), (1, 2), 
                         rowspan = 3)

axes4 = plt.subplot2grid((4, 4), (2, 0))
axes5 = plt.subplot2grid((4, 4), (2, 1))

fig.tight_layout()
Output : Example 2: Python3 1==
import random
import matplotlib.pyplot as plt
from matplotlib import style


style.use('fivethirtyeight')

fig = plt.figure()


# helper function to plot the lines
def helper():
    
    xs = []
    ys = []

    for i in range(10):
        x = i
        y = random.randrange(10)

        xs.append(x)
        ys.append(y)
    return xs, ys

axes1 = plt.subplot2grid ((7, 1), (0, 0),
                          rowspan = 2, 
                          colspan = 1)

axes2 = plt.subplot2grid ((7, 1), (2, 0),
                          rowspan = 2,
                          colspan = 1)

axes3 = plt.subplot2grid ((7, 1), (4, 0), 
                          rowspan = 2, 
                          colspan = 1)

x, y = helper()
axes1.plot(x, y)

x, y = helper()
axes2.plot(x, y)

x, y = helper()
axes3.plot(x, y)
Output:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4