【问题标题】:matplotlib legend label made of line and picture loses line when moving the legend out of the figure将图例移出图形时,由线条和图片组成的matplotlib图例标签丢失线条
【发布时间】:2018-11-01 22:35:40
【问题描述】:

我想用一张图片和一条线重现这个标签(参见:Replace Matplotlib legend's labels with image),但是当我尝试使用 bbox_to_anchor 将它移到轴外时,标签中的线消失了,而图片仍然存在。什么都不会被切断。下面是相关的代码部分。因为我有不止一行,所以我使用行列表和图例列表(后者是空的)。

plt.legend(handles = lineliste, labels = legendliste, handler_map = 
handler_map, handlelength=4, labelspacing=0.0, fontsize=36, borderpad=0.1, 
handletextpad=0.2, borderaxespad=0.1, handleheight=2, loc="upper center", 
frameon = False, bbox_to_anchor=(2.02,1))

@ImportanceOfBeingErnest 您是否有解决方案,因为您提供了第一个问题的答案?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    抱歉,original solution 中有一个

    l.set_clip_on(False) 
    

    不见了。我编辑了那个答案。它现在看起来像

    bbox_to_anchor=(1.1,1) 添加到图例调用时。

    【讨论】:

    • 谢谢!有没有办法调整盒子内处理程序的位置?现在底部的距离大于顶部的距离。在我的情况下,如果有更多条目,这会导致某些条目不在框内
    • 我相信这是可能的;但我不知道问题是什么。见minimal reproducible example
    • 我根据给出的建议发布了一个新问题 (stackoverflow.com/questions/53145003/…)。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多