matplotlib.figure.Figure.add_gridspec#

Figure.add_gridspec(nrows=1, ncols=1, **kwargs)[源代码]#

用于创建以该图形为父级的 GridSpec 的低级 API。

这是一个低级API,允许你创建一个gridspec并在其基础上添加子图。大多数用户不需要这种自由度,应该使用更高级的方法 subplotssubplot_mosaic

参数:
nrowsint, 默认值: 1

网格中的行数。

ncolsint, 默认值: 1

网格中的列数。

返回:
GridSpec
其他参数:
**kwargs

关键字参数传递给 GridSpec

示例

添加一个跨越两行的子图:

fig = plt.figure()
gs = fig.add_gridspec(2, 2)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[1, 0])
# spans two rows:
ax3 = fig.add_subplot(gs[:, 1])

使用 matplotlib.figure.Figure.add_gridspec 的示例#

带有直方图的散点图

Scatter plot with histograms

使用紧凑布局调整坐标轴大小

Resize Axes with tight layout

使用 plt.subplots 创建多个子图

Create multiple subplots using plt.subplots

使用 axisartist 自定义轴脊

Custom spines with axisartist

嵌套的 GridSpecs

Nested GridSpecs

约束布局指南

Constrained layout guide

在图形中排列多个轴

Arranging multiple Axes in a Figure