Matplotlib.pyplot.subplot2grid() in python Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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. Matplotlib.pyplot.subplot2grid() 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 : shape: As the name suggests it is used to define the shape of the grid to be plotted within the graph. It is a required argument and is generally passed in as a list or tuple of two numbers which are responsible for the layout of the grid with the first number being the number of rows and the second number as the number of columns. location (loc): This is the second mandatory argument that this function takes. Similar to the shape argument it is also a required argument and is generally passed in as a list or tuple of two numbers. It is used for specifying the row and column number to place the sub-plot. It is also important to note that the indexes start from 0. So (0, 0) would be the cell in the first row and the first column of the grid. rowspan: Once the grid layout is set and the starting index is decided using location(loc) one can expand the selection to take up more rows with this argument. This is an optional parameter and has a default value of 1. colspan: Similar to rowspan it is used to expand the selection to take up more columns. It is also an optional parameter with default value of 1. 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: Comment More info R rajukumar19 Follow Improve Article Tags : Python Write From Home Python-Library Python-matplotlib Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like