How to control the legend in Seaborn - Python

后端 未结 2 956
渐次进展
渐次进展 2021-01-14 06:15

I am trying to find guidance on how to control and customize the legend in Seaborn plots but I can not find any.

To make the issue more concrete I provide a reproduc

2条回答
  •  花落未央
    2021-01-14 07:00

    First, to access the legend created by seaborn needs to be done via the seaborn call.

    g = sns.factorplot(...)
    legend = g._legend
    

    This legend can then be manipulated,

    legend.set_title("Sex")
    for t, l in zip(legend.texts,("Male", "Female")):
        t.set_text(l)
    

    The result is not totally pleasing because the strings in the legend are larger than previously, hence the legend would overlap the plot

    One would hence also need to adjust the figure margins a bit,

    g.fig.subplots_adjust(top=0.9,right=0.7)
    

提交回复
热议问题