Python - Matplotlib:子图的figsize - 在行之间添加间距

18

我正在使用以下代码来制作图表:

   fig, axes = plt.subplots(len(pairs), 3, figsize=(12, 324))
    for i, pair in enumerate(pairs):
        d = pd.DataFrame(columns=[pairs[0], pairs[1]])
        e = df_norm[list(pair)]
        ax0 = axes[i,0]
        ax1 = axes[i,1]
        ax2 = axes[i,2]

        d[pair[0]] = np.random.normal(e[pair[0]],0.02)
        d[pair[1]] = np.random.normal(e[pair[1]],0.02)

    d.plot.scatter(*pair, ax=ax0, c=col0, linewidths=0, s=2, alpha = 0.7)
    d.plot.scatter(*pair, ax=ax1, c=col1, linewidths=0, s=2, alpha = 0.7)
    d.plot.scatter(*pair, ax=ax2, c=col2, linewidths=0, s=2, alpha = 0.7)

fig.tight_layout()

然而,我的输出图表看起来非常混乱,如下所示:

在此输入图像描述

我尝试通过调整 figsize=(12, 324) 中的324的大小,但是两种方法都没有帮助。有没有一种方法可以在每行之间放置一些空白,这样图形就不会混乱了。谢谢!


1
请提供一个 [MCVE]。 - ImportanceOfBeingErnest
2个回答

37
你需要省略fig.tight_layout(),并使用subplots_adjust代替。
plt.subplots_adjust(top = 0.99, bottom=0.01, hspace=1.5, wspace=0.4)

存在一些非常极端的数值。 hspace 是子图之间的垂直间隔(很可能以子图高度为单位)。

Figure 有相同的 subplots_adjust 方法,因此您可以决定选择哪一个。


13

在我看来,最简单的方法是使用fig.tight_layout,并使用h_pad(垂直方向)和w_pad(水平方向)来设置边距。例如:

fig.tight_layout(h_pad=5, w_pad=5)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接