【问题标题】:Huge margin when using matplotlib supxlabel使用 matplotlib supxlabel 时的巨大利润
【发布时间】:2023-02-16 23:01:38
【问题描述】:

我试图在 matplotlib 的子图中添加一个公共标签,但我遇到了一些麻烦。

我正在使用 python 3.10 和 matplotlib 3.5.1

有一个最小的工作示例说明了这个问题:

import matplotlib.pyplot as plt
fig, axs = plt.subplots(3, 2, figsize=(8, 12),  sharex=True, sharey=True)
fig.supxlabel('Example of supxlabel')
fig.supylabel('Example of supylabel')
fig.subplots_adjust(wspace=0, hspace=0)
plt.savefig('test.pdf', bbox_inches='tight', pad_inches=0)

此代码生成下图:

请注意'Example of supxlabel''Example of supylabel' 上方巨大的丑陋边距。

我尝试使用选项 constrained_layout=Truefig.set_constrained_layout_pads,但它没有解决我的问题。

我知道可以使用xyvaha中的supxlabelsupylabel来解决问题,但我有很多数字要生成,无法实际找到和设置值这些选项手动。

【问题讨论】:

  • 尝试 plt.tight_layout(),您可以检查可以自动改变不同垫的选项

标签: python matplotlib


【解决方案1】:

我目前的解决方法是y = np.sqrt(height) / 100 * 2.2,它似乎至少对height从 5 到 25 有效。如果使用subplots_adjust(bottom=),一个恒定的调整因子似乎有效,比如y -= .04。我开了一个Issue

width = 10
height = 20
y = np.sqrt(height) / 100 * 2.2
fig, _ = plt.subplots(23, 1, figsize=(width, height))
fig.supxlabel("supxlabel", y=y)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    相关资源
    最近更新 更多