Last Updated : 12 Jul, 2025
Matplotlibis a library in Python and it is numerical – mathematical extension for NumPy library. The
figure moduleprovides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot elements.
matplotlib.figure.Figure.add_gridspec() function The add_gridspec() methodfigure module of matplotlib library is used to get the GridSpec that has this figure as a parent.
Syntax: add_gridspec(self, nrows, ncols, **kwargs) Parameters: This accept the following parameters that are described below:Returns: This method return the GridSpec.
- nrows : This parameter is the number of rows in grid.
- ncols : This parameter is the number or columns in grid.
Below examples illustrate the matplotlib.figure.Figure.add_gridspec() function in matplotlib.figure:
Example 1: Python3
# Implementation of matplotlib function
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout = True)
gs = fig.add_gridspec(3, 3)
ax = fig.add_subplot(gs[0, :])
ax.set_title('gs[0, :]')
ax2 = fig.add_subplot(gs[1, :-1])
ax2.set_title('gs[1, :-1]')
fig.suptitle('matplotlib.figure.Figure.add_gridspec() \
function Example\n\n', fontweight ="bold")
plt.show()
Output: Example 2: Python3
# Implementation of matplotlib function
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure()
gs = fig.add_gridspec(2, 2)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[1, 0])
ax3 = fig.add_subplot(gs[:, 1])
fig.suptitle('matplotlib.figure.Figure.add_gridspec()\
function Example\n\n', fontweight ="bold")
plt.show()
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