Python中的 Matplotlib.figure.Figure.add_gridspec()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 figure 模块提供了顶级 Artist,即 Figure,其中包含所有绘图元素。该模块用于控制所有绘图元素的子图和顶级容器的默认间距。
matplotlib.figure.Figure.add_gridspec()函数
matplotlib库的add_gridspec()方法figure模块用于获取以该figure为父对象的GridSpec。
Syntax: add_gridspec(self, nrows, ncols, **kwargs)
Parameters: This accept the following parameters that are described below:
- nrows : This parameter is the number of rows in grid.
- ncols : This parameter is the number or columns in grid.
Returns: This method return the GridSpec.
下面的示例说明了 matplotlib.figure 中的 matplotlib.figure.Figure.add_gridspec()函数:
示例 1:
# 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()
输出:
示例 2:
# 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()
输出: