简体   繁体   English

tight_layout() 保留 ylabel 和 yticklabels 空间,即使在禁用时也是如此

[英]tight_layout() reserves ylabel and yticklabels space even when disabled

I have a 2*4 subplots figure, with half of the ylabel and yticklabels disabled.我有一个 2*4 子图,禁用了一半的 ylabel 和 yticklabels。 Unfortunately, tight_layout() does not remove the extra white space which corresponds to the area where the ylabel and yticklabels would appear if they were not disabled.不幸的是,tight_layout() 不会删除额外的空白区域,这些空白区域对应于 ylabel 和 yticklabels 在未禁用的情况下会出现的区域。 The ylabel and yticklabels are removed because I would like to have 4 pairs of comparison subplots. ylabel 和 yticklabels 被删除,因为我想要 4 对比较子图。 The plot looks something like this. plot 看起来像这样。

在此处输入图像描述

I am looking for an efficient way to remove the extra white space.我正在寻找一种有效的方法来删除多余的空白区域。 In fact, I would like each pair of plots to be next to each other with no space at all.事实上,我希望每一对地块都紧挨着,完全没有空间。 Here is an working example.这是一个工作示例。

import matplotlib.pyplot as plt

fig, ((ax0, ax1, ax2, ax3), (ax4, ax5, ax6, ax7)) = plt.subplots(2, 4, figsize=(8, 4))
axs = [ax0, ax1, ax2, ax3, ax4, ax5, ax6, ax7]

for i in range(4):
    axs[2*i].set_ylabel('parameter '+str(i))
    axs[2*i+1].set_yticklabels([])

plt.tight_layout()
plt.show()

The code should yield the above plot. Any tips would be appreciated.该代码应产生上述 plot。任何提示将不胜感激。 Many thanks!非常感谢!

You can do it by having two subgrids and forcing a null distance between the axis (I followed this tutorial ).您可以通过拥有两个子网格并强制轴之间的距离为 null 来实现(我遵循了本教程)。 The wspace parameter is described here . 此处描述了wspace参数。

import matplotlib.pyplot as plt

fig = plt.figure(constrained_layout=False) # you must disable automatic spacing
gs = fig.add_gridspec(1, 2, figure=fig)

gs0 = gs[0].subgridspec(2, 2, wspace=0.0)
gs1 = gs[1].subgridspec(2, 2, wspace=0.0)

axs = [[],[]]
for i in range(2):
    for j in range(2):
        axs[0].append(fig.add_subplot(gs0[i, j]))
        axs[1].append(fig.add_subplot(gs1[i, j]))

axs = [*axs[0],*axs[1]]

for i in range(4):
    axs[2*i].set_ylabel('parameter ' + str(i))
    axs[2*i+1].set_yticklabels([])

使用 gridspec 和 subgridspec 的示例图像

Since the automatic spacing is disabled, you may have to play with axis and figure properties to adapt the position of the axis, labels, etc.由于禁用了自动间距,您可能必须使用轴和图形属性来调整轴、标签等的 position。

A partial (distance won't be null, but may be interesting for other users), more elegant solution is to use constrained_layout when creating the figure.部分(距离不会是 null,但其他用户可能会感兴趣),更优雅的解决方案是在创建图形时使用constrained_layout More info in matplotlib's documentation . matplotlib 文档中的更多信息。

import matplotlib.pyplot as plt

fig, axs = plt.subplots(
    2, 4,
    figsize=(8, 4),
    constrained_layout=True,
    )

axs = axs.reshape([8])
for i in range(4):
    axs[2*i].set_ylabel('parameter '+str(i))
    axs[2*i+1].set_yticklabels([])

# plt.tight_layout() # not necessary
plt.show()

使用约束布局的示例图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM