matplotlib 为什么添加轴到subgridspec与参数混乱< wspace>?

qlfbtfca  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(106)

我有一个包含4个子图和2个重叠子图的网格,以在前两个和后两个子图之间创建公共xlabels(参见下面的代码)。虽然参数设置为相等值,但左侧的两个子图与右侧的两个子图之间的间距略有不同,为什么?
如果未添加重叠轴,则间距与预期一致。下面还有一个链接与图像的例子.
Result of below code and example of the problem

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
# make the grid
fig = plt.figure(constrained_layout=True, figsize=(8,3))
grsp = GridSpec(1,4, figure=fig, wspace=0.6, width_ratios=[1,0.05,1,0.05])

grsp_one = grsp[0,0].subgridspec(1,2, wspace=0.01)
grsp_two = grsp[0,2].subgridspec(1,2, wspace=0.01)

axs_one = grsp_one.subplots(sharey='row')
axs_two = grsp_two.subplots(sharey='row')
# create the ovelapping ax on selection of axes
axmaster_1 = fig.add_subplot(grsp_one[0,0:2])
axmaster_2 = fig.add_subplot(grsp_two[0,0:2])

for axmaster in [axmaster_1, axmaster_2]:
    # hide the ticks of the master axis
    axmaster.xaxis.set_major_locator(matplotlib.ticker.NullLocator())
    axmaster.yaxis.set_major_locator(matplotlib.ticker.NullLocator())
    axmaster.spines[['bottom', 'right', 'top', 'left']].set_visible(False)
    axmaster.set_zorder(-10)

axmaster_1.set_xlabel('xlabel_1', labelpad=20)
axmaster_2.set_xlabel('xlabel_2', labelpad=20)

axmaster_1.set_ylabel('ylabel_1', labelpad=30)
axmaster_2.set_ylabel('ylabel_2', labelpad=30)

plt.show()
5rgfhyps

5rgfhyps1#

正如@MattPitkin所建议的那样,使用更新的matplotlib版本设置constrained_layout=False解决了不相等空间的问题,但需要对gridspec和subgridspec的wspace, hspace, left, bottom, right, top进行一些调整,以使图看起来更好看。

相关问题