【问题标题】:Matplotlib - place legend below plot and close to the figure edgeMatplotlib - 将图例放在图下方并靠近图形边缘
【发布时间】:2021-05-31 01:13:46
【问题描述】:

我有一个固定大小 (1000*1000) 的图形,我试图在图下方放置一个图例(即在轴边界框之外)。我使用bbox_to_anchor 参数到plt.legend()pad 参数到plt.tight_layout()。问题是,当我尝试将图例进一步向下移动时,通过减小传递给bbox_to_anchor 的第二个值,图形底部边缘和图例之间的填充会增加,即使我在plt.tight_layout() 中设置了pad=0 .我想使用 0.7 的填充,以使绘图与使用该填充的其他绘图一致,并使图例与条形标签充分分开,将绘图的缩小保持在所需的最小值。我已经阅读了matplotlib documentation legend guidethis excelent answer 到一个相关问题,但我不明白为什么图例底部和图形底部之间的填充会随着我减小 2- 的 y 值而增加元组传递给 bbox_to_anchor 参数。我也试图弄乱loc 参数,但无济于事。

示例

案例一

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.3), fontsize=30, frameon=True, borderaxespad=0)
    
plt.tight_layout(pad=0)

案例2

试图将图例进一步向下移动:

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.5), fontsize=30, frameon=True, borderaxespad=0)
        
plt.tight_layout(pad=0)

案例3

将内边距更改为 0.2,这样图形的边缘就不会被剪裁:

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.5), fontsize=30, frameon=True, borderaxespad=0)
    
plt.tight_layout(pad=0.2)

创建绘图的函数代码(删除了一些不必要的部分):

def example_plot(left_total, right_total, target):
    
    fig, ax = plt.subplots(figsize=(10,10), linewidth=2, edgecolor='#9E9E9E')

    names = ['left', 'right']
        
    size_of_groups = [left_total, right_total]
    
    left_bar = ax.bar(names[0], size_of_groups[0], color='#515151', width=0.5)
    right_bar = ax.bar(names[1], size_of_groups[1], color='#FFD732', width=0.5)

    ax.axhline(y=target, color='#32CDFF', linestyle='solid', lw=3, label='Target')
        
    ymin = min(left_total, right_total, target)
    
    ymin = math.floor(ymin/1000)*1000
    ymax = max(left_total, right_total, target)       
    ymax = math.ceil(ymax/1000)*1000
    ax.set_ylim([ymin, ymax])       
        
    yticks = list(range(ymin, ymax+1000, 1000))        
    ax.set_yticks(yticks)

    ax.bar_label(left_bar, fontsize=40)
    ax.bar_label(right_bar, fontsize=40)
    
    ax.spines[:].set_visible(False)

    ax.set_axisbelow(True)
    ax.grid(True, axis='y', color='#9E9E9E', clip_on=False)

    ax.set_title("Example plot\n", fontsize=50, fontweight = 500)

    ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.3), fontsize=30, frameon=True, borderaxespad=0)
    
    plt.tight_layout(pad=0)
    
    fig.savefig("plots/example_plot.png")

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:
    1. bbox 参数在坐标区坐标中,因此 0.3 是坐标区高度的 0.3。
    2. tight_layout 的 pad 参数是平均轴大小的分数,所以如果它是 0.7,pad 将是轴高度的 70%。我不知道你为什么想要 0.7,但是如果你让 bbox 从 -0.3 向下移动到 -0.5,轴会因为tight_layout 而变小,因此焊盘会变小。

    主动提供的建议:tight_layout 和 constrained_layout 用于简单的默认情况。它们不是无限灵活的,所以如果您有非常特殊的需求,我建议您学习如何手动放置轴和图例。

    【讨论】:

    • 感谢您的回答!你确定垫是轴尺寸的一小部分吗?因为documentation specifically says是“字体大小的分数”,默认值为1.08。我一直在将pad=0.7 用于其他地块,它肯定没有添加轴高度的 70% 的填充。你会说什么是在轴之外向上或向下移动图例而不影响图例和图形边缘之间的填充的“标准”方式?
    • 对不起,你是对的。 pad 论点是一个红鲱鱼。轴外的图例是紧密布局的问题。但是,如果您想将图例放置在图形坐标中,则将 bbox_transform 更改为 fig.transFigure。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2016-10-02
    • 1970-01-01
    • 2021-09-01
    相关资源
    最近更新 更多