matplotlib 调整两个轴之间的空间,同时保持其他轴上的空间不变

bqucvtff  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(112)

由于某种原因,我找不到关于这方面的信息(我很确定它存在于某个地方),但在下面的通用示例中,我想减少ax 1和ax 2之间的hspace,同时保持ax 2-ax3和ax3-ax 4之间的hspace不变。
我也很感激任何链接到这样的例子!

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

def annotate_axes(fig):
    for i, ax in enumerate(fig.axes):
        ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
        ax.tick_params(labelbottom=False, labelleft=False)

fig = plt.figure()

gs1 = GridSpec(6, 1, hspace=0.2)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])

ax3 = fig.add_subplot(gs1[2:4])
ax4 = fig.add_subplot(gs1[4:6])

annotate_axes(fig)
plt.show()

pkbketx9

pkbketx91#

一种可能适合您需要的方法是创建一个子网格(在本例中,将hspace设置为0):

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

def annotate_axes(fig):
    for i, ax in enumerate(fig.axes):
        ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
        ax.tick_params(labelbottom=False, labelleft=False)

fig = plt.figure()

gs1 = GridSpec(6, 1, hspace=0.2)

# subgrid for the first two slots
# in this example with no space
subg = gs1[0:2].subgridspec(2, 1, hspace = 0)

# note the ax1 and ax2 being created from the subgrid
ax1 = fig.add_subplot(subg[0])
ax2 = fig.add_subplot(subg[1])

ax3 = fig.add_subplot(gs1[2:4])
ax4 = fig.add_subplot(gs1[4:6])

annotate_axes(fig)
plt.show()

相关问题