注意
跳转到末尾以下载完整示例代码。
使用 subplots 和 GridSpec 组合两个子图#
有时我们希望在用 subplots
创建的 Axes 布局中组合两个子图。我们可以从 Axes 中获取 GridSpec
,然后移除被覆盖的 Axes 并用一个新的更大的 Axes 填充该空白。在这里,我们创建了一个布局,其中最后一列的底部两个 Axes 已组合。
要使用此布局(而不是移除重叠的 Axes),请使用 subplot_mosaic
。
另请参阅 在图中排列多个 Axes。
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()

脚本总运行时间: (0 分钟 1.489 秒)