Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove seaborn lineplot legend title

I would like to remove the title from my seaborn lineplot legend. I tried using this answer to no avail:

import matplotlib.pyplot as plt import seaborn as sns; sns.set() fmri = sns.load_dataset("fmri") fig, ax = plt.subplots() g = sns.lineplot(x="timepoint", y="signal", hue="event", data=fmri, ax=ax) ax.legend().set_title('') 

Seaborn lineplot still with 'event' title

I get the same if I try to set the title to None. Interestingly, setting the title to something else seems to prepend to the existing title:

ax.legend().set_title('Something else') 

Seaborn lineplot still with prepend title

It almost looks like seaborn is treating the title as a hidden legend entry. How can I resolve this?

like image 709
Daniel Avatar asked Jul 29 '18 10:07

Daniel


People also ask

How do you delete a legend in Seaborn Lineplot?

You can remove each legend for the first three axes, and then use plt. figlegend() which is a legend for the entire figure. You might have to adjust the bbox_to_anchor() arguments based on what is in your legend.

How do I edit my legend in Seaborn?

To change the position of a legend in a seaborn plot, you can use the plt. legend() command. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.

How do I change my legend color in Seaborn?

seaborn turns the legend frame off by default, if you want to customize how the frame looks, I think you'll need to add frameon=True when you call plt. legend . Show activity on this post. The set_style() method can take a style argument (e.g. 'white' , 'whitegrid' , 'darkgrid' , etc.)

How do you add a legend title in Seaborn?

By default, seaborn automatically adds a legend to the graph. Notice the legend is at the top right corner. If we want to explicitly add a legend, we can use the legend() function from the matplotlib library.


2 Answers

Important: This answer is about the case when a hue is used that appears as a legend title. In all other cases, the question itself already contains the usual way to get rid of a title.

Indeed, seaborn is misusing a legend label as a (subgroup-)title. Hence the idea can be to either remove this label, or replace it with custom text.

Replacing with custom text:

legend = ax.legend() legend.texts[0].set_text("Whatever else") 

enter image description here

Removing the label:

handles, labels = ax.get_legend_handles_labels() ax.legend(handles=handles[1:], labels=labels[1:]) 

enter image description here

After having removed the label you may of course still set another (real) title:

handles, labels = ax.get_legend_handles_labels() ax.legend(handles=handles[1:], labels=labels[1:], title="Whatever else") 

enter image description here

like image 131
ImportanceOfBeingErnest Avatar answered Sep 22 '22 16:09

ImportanceOfBeingErnest


import seaborn as sns g = sns.lineplot(x="myXs", y="myYs", hue="myHue", data=mydf) g.legend_.set_title(None) 
like image 24
ofornes Avatar answered Sep 24 '22 16:09

ofornes