python 中带有 gridspec.GridSpec 的变量 wspace

spo*_*nce 3 python matplotlib

我想在 matplotlib 中使用 GridSpec 创建一个变量(两个不同的)wspace。

我想实现以下目标: 目标

到目前为止我正在使用以下内容:

gs1 = gridspec.GridSpec(6, 3, width_ratios=[1.5,1,1])
gs1.update(wspace=0.4, hspace=0.3)
ax1 = fig.add_subplot(gs1[0:2,0])
ax2 = fig.add_subplot(gs1[2:4,0])
ax3 = fig.add_subplot(gs1[4:6,0])
ax4 = fig.add_subplot(gs1[0:3,1])
ax5 = fig.add_subplot(gs1[3:6,1])
ax6 = fig.add_subplot(gs1[0:3,2])
ax7 = fig.add_subplot(gs1[3:6,2])
Run Code Online (Sandbox Code Playgroud)

知道如何在我令人惊叹的手绘图中获得以绿色突出显示的两个不同空间吗?

多谢 !

山姆

Imp*_*est 9

您可以使用 2 个 GridSpec,其中一个包含 1 列和 3 行,另一个包含 2 行和 2 列。然后,您可以让第一个仅延伸到图形宽度的一半以下,并从图形宽度的一半开始第二个。左右参数之间的差异将是间距。

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs1 = GridSpec(3, 1, right=0.4)
gs2 = GridSpec(2, 2, left=0.5)


ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[1,0])
ax3 = fig.add_subplot(gs1[2,0])
ax4 = fig.add_subplot(gs2[0,0])
ax5 = fig.add_subplot(gs2[0,1])
ax6 = fig.add_subplot(gs2[1,0])
ax7 = fig.add_subplot(gs2[1,1])

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

通过首先定义具有两列的“外部”网格规范并将内部网格规范放入其中的每一列,可以实现相同的效果。

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec

fig = plt.figure()
gs = GridSpec(1, 2, width_ratios=[1.5,2], wspace=0.3)

gs1 = GridSpecFromSubplotSpec(3, 1, subplot_spec=gs[0])
gs2 = GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])

ax1 = fig.add_subplot(gs1[0,0])
ax2 = fig.add_subplot(gs1[1,0])
ax3 = fig.add_subplot(gs1[2,0])
ax4 = fig.add_subplot(gs2[0,0])
ax5 = fig.add_subplot(gs2[0,1])
ax6 = fig.add_subplot(gs2[1,0])
ax7 = fig.add_subplot(gs2[1,1])

plt.show()
Run Code Online (Sandbox Code Playgroud)