Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: Unknown property legend in seaborn

Tags:

The seaborn stripplot has a function which allows hue.

Using the example from https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.stripplot.html

import seaborn as sns sns.set_style("whitegrid") tips = sns.load_dataset("tips") ax = sns.stripplot(x=tips["total_bill"]) ax = sns.stripplot(x="sex", y="total_bill", hue="day", data=tips, jitter=True) 

enter image description here

In this case, the legend is quite small, showing a different hue for each day. However, I would like to remove the legend.

Normally, one includes a parameter legend=False. However, for stripplot, this appears to output an attribute error:

AttributeError: Unknown property legend 

Can one remove the legend for stripplots? If so, how does one do this?

like image 463
ShanZhengYang Avatar asked Jun 30 '16 18:06

ShanZhengYang


People also ask

How do I add a legend to my Seaborn plot?

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. In this way, we can add our own labels explicitly.

How do you hide the legend in Seaborn?

Through the legend parameter set to false and by using the legend function and remove function, the seaborn legend can be easily removed.

How do you get rid of the legend in Seaborn Boxplot?

Use the remove() Function to Remove the Legend From a Seaborn Plot in Python. This method works with the objects belonging to different classes like the PairGrid class from the seaborn module. We can call the legend using the _legend() function and remove it using the remove() method.


1 Answers

Use ax.legend_.remove() like here:

import seaborn as sns import matplotlib.pylab as plt sns.set_style("whitegrid") tips = sns.load_dataset("tips") ax = sns.stripplot(x="sex", y="total_bill", hue="day", data=tips, jitter=True)  # remove legend from axis 'ax' ax.legend_.remove()  plt.show() 

enter image description here

like image 90
Serenity Avatar answered Oct 03 '22 02:10

Serenity