Edit seaborn legend

后端 未结 3 918
既然无缘
既然无缘 2020-11-29 21:14

Using a data frame and this code in Python, I was able to create a plot:

g = sns.lmplot(\'credibility\', \'percentWatched\', data=data, hue = \'millennial\',         


        
相关标签:
3条回答
  • 2020-11-29 21:58

    If legend_out is set to True then legend is available thought g._legend property and it is a part of a figure. Seaborn legend is standard matplotlib legend object. Therefore you may change legend texts like:

    import seaborn as sns
    
    tips = sns.load_dataset("tips")
    g = sns.lmplot(x="total_bill", y="tip", hue="smoker",
     data=tips, markers=["o", "x"], legend_out = True)
    
    # title
    new_title = 'My title'
    g._legend.set_title(new_title)
    # replace labels
    new_labels = ['label 1', 'label 2']
    for t, l in zip(g._legend.texts, new_labels): t.set_text(l)
    
    sns.plt.show()
    

    Another situation if legend_out is set to False. You have to define which axes has a legend (in below example this is axis number 0):

    import seaborn as sns
    
    tips = sns.load_dataset("tips")
    g = sns.lmplot(x="total_bill", y="tip", hue="smoker",
     data=tips, markers=["o", "x"], legend_out = False)
    
    # check axes and find which is have legend
    leg = g.axes.flat[0].get_legend()
    new_title = 'My title'
    leg.set_title(new_title)
    new_labels = ['label 1', 'label 2']
    for t, l in zip(leg.texts, new_labels): t.set_text(l)
    sns.plt.show()
    

    Moreover you may combine both situations and use this code:

    import seaborn as sns
    
    tips = sns.load_dataset("tips")
    g = sns.lmplot(x="total_bill", y="tip", hue="smoker",
     data=tips, markers=["o", "x"], legend_out = True)
    
    # check axes and find which is have legend
    for ax in g.axes.flat:
        leg = g.axes.flat[0].get_legend()
        if not leg is None: break
    # or legend may be on a figure
    if leg is None: leg = g._legend
    
    # change legend texts
    new_title = 'My title'
    leg.set_title(new_title)
    new_labels = ['label 1', 'label 2']
    for t, l in zip(leg.texts, new_labels): t.set_text(l)
    
    sns.plt.show()
    

    This code works for any seaborn plot which is based on Grid class.

    0 讨论(0)
  • 2020-11-29 22:01

    If you just want to change the legend title, you can do the following:

    import seaborn as sns
    import matplotlib.pyplot as plt
    tips = sns.load_dataset("tips")
    
    g = sns.lmplot(
        x="total_bill", 
        y="tip", 
        hue="smoker", 
        data=tips,  
        legend=True
    )
    
    g._legend.set_title("New Title")
    
    0 讨论(0)
  • 2020-11-29 22:09

    Took me a while to read through the above. This was the answer for me:

    import seaborn as sns
    import matplotlib.pyplot as plt
    tips = sns.load_dataset("tips")
    
    g = sns.lmplot(
        x="total_bill", 
        y="tip", 
        hue="smoker", 
        data=tips,  
        legend=False
    )
    
    plt.legend(title='Smoker', loc='upper left', labels=['Hell Yeh', 'Nah Bruh'])
    plt.show(g)
    

    Reference this for more arguments: matplotlib.pyplot.legend

    0 讨论(0)
提交回复
热议问题