首页 > 解决方案 > 通过多次单击“调整”->“紧密布局”以交互方式使无花果布局更紧密,但无法在我的代码中重现它

问题描述

一件奇怪的事情正在发生。通过多次单击“调整”->“紧密布局”,我在显示绘图后以交互方式更改图形的紧密程度。我知道我可以复制这 6 个参数并使用它们放入我的代码fig.subplots_adjust()中,但问题是布局发生了变化,但有一点参数没有!所以,我无法重现效果...... 在这里,我将向您展示我的意思:

在我的代码中,我plt.tight_layout()之前有plt.show().

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

    plt.subplots_adjust(
              top=0.95,
              bottom=0.062,
              left=0.012,
              right=0.988,
              hspace=0.249,
              wspace=0.0)

我得到:

在此处输入图像描述

身材没有我想的那么紧!!而且次要情节没有我想要的那么大!!我把完全相同的参数。任何人都知道如何解决它?


重现问题的玩具示例:

import matplotlib.pyplot as plt
limit = 3
fig, axes = plt.subplots(nrows=limit, ncols=limit, sharex=True, sharey=True, )

for i in range(limit):
    for j in range(limit):
        ax = axes[i, j]
        ax.set_xbound(lower=-2, upper=2)
        ax.set_ybound(lower=-2, upper=2)
        ax.grid()
        ax.set_aspect('equal', adjustable='box')

# show
fig = plt.gcf()
fig.canvas.manager.window.showMaximized()
fig.canvas.set_window_title('Test')
plt.tight_layout() # TODO: comment this line to see the difference if tight layout is not specified

# TODO: uncomment the following block to see that nothing happens if tight layout and the parameters are set
# plt.subplots_adjust(
#   top=0.951,
#   bottom=0.062,
#   left=0.012,
#   right=0.988,
#   hspace=0.249,
#   wspace=0.0
# )
plt.show()

旁注:我目前正在使用matplotlib 3.1.1

标签: pythonmatplotliblayoutsubplot

解决方案


推荐阅读