使用 subplots 和 GridSpec 组合两个子图#

有时我们想在一个使用 subplots 创建的 Axes 布局中合并两个子图。我们可以从 Axes 中获取 GridSpec,然后移除被覆盖的 Axes 并用一个新的更大的 Axes 填充空隙。这里我们创建了一个布局,将最后一列的底部两个 Axes 合并。

要使用此布局(而不是移除重叠的 Axes),请使用 subplot_mosaic

另请参阅 在图形中排列多个轴

gridspec and subplots
import matplotlib.pyplot as plt

fig, axs = plt.subplots(ncols=3, nrows=3)
gs = axs[1, 2].get_gridspec()
# remove the underlying Axes
for ax in axs[1:, -1]:
    ax.remove()
axbig = fig.add_subplot(gs[1:, -1])
axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
               xycoords='axes fraction', va='center')

fig.tight_layout()

plt.show()

由 Sphinx-Gallery 生成的图库