Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide legend from seaborn pairplot

I would like to hide the Seaborn pairplot legend. The official docs don't mention a keyword legend. Everything I tried using plt.legend didn't work. Please suggest the best way forward. Thanks!

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

test = pd.DataFrame({
    'id': ['1','2','1','2','2','6','7','7','6','6'],
    'x': [123,22,356,412,54,634,72,812,129,110],
    'y':[120,12,35,41,45,63,17,91,112,151]})
sns.pairplot(x_vars='x', y_vars="y", 
                 data=test,
                 hue = 'id', 
                 height = 3)
like image 753
aviss Avatar asked Feb 20 '19 07:02

aviss


People also ask

How do I hide my legend in Seaborn?

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.

How do you remove the legend from a Boxplot in Python?

Example 1: By using ax. legend_ = None, legend can be removed from figure in matplotlib.

How do you change the legend in Seaborn?

Changing the location of Seaborn legends We use matplotlib. pyplot. legend() function from matplotlib library and pass the bbox_to_anchor parameter which allows us to pass an (x,y) tuple with the required offset for changing the location of the seaborn legends.

How do I delete a scatterplot legend?

We could use remove() and set_visible() methods of the legend object to remove legend from a figure in Matplotlib. We can also remove legend from a figure in Matplotlib by setting the label to _nolegend_ in plot() method, axes. legend to None and figure.

How to remove the legend from a seaborn plot in Python?

In this tutorial, we will learn how to remove the legend from a seaborn plot in Python. Most of the plot functions in seaborn accept the legend parameter. We can set it to False and hide the legend from the final plot. The matplotlib.pyplot.legend () function can be used to add a custom legend to seaborn plots.

What is Seaborn pairplot in Python?

Seaborn.pairplot uses many arguments as input, main of which are described below in form of table: Tidy (long-form) dataframe where each column is a variable and each row is an observation. Variable in “data“ to map plot aspects to different colors. Set of colors for mapping the “hue“ variable.

What is Seaborn legend | how to add and change?

Seaborn Legend | How to Add and Change Seaborn Legend? Seaborn legend is a dialog box located in the graph and includes the different attribute descriptions with the graph of respected colors. Seaborn library in python is making graphics on top of matplotlib with the data structures of pandas.

How to remove the legend from a pairplot in Matplotlib?

We can call the legend using the _legend () function and remove it using the remove () method. See the code below. The pairplot () function returns an object of PairGrid class.


3 Answers

Since _legend.remove() method won't work on some other seaborn plots, what about:

plt.legend([],[], frameon=False)
like image 143
SOJA Avatar answered Oct 07 '22 06:10

SOJA


You need to return the Seabron Pairgrid object when you use pairplot and then you can access the legend of the Pairgrid using ._legend. Then simply call remove():

import seaborn as sns

test = pd.DataFrame({
    'id': ['1','2','1','2','2','6','7','7','6','6'],
    'x': [123,22,356,412,54,634,72,812,129,110],
    'y':[120,12,35,41,45,63,17,91,112,151]})

g = sns.pairplot(x_vars='x', y_vars="y", data=test, hue = 'id', height = 3)
g._legend.remove()

enter image description here

like image 41
DavidG Avatar answered Oct 07 '22 05:10

DavidG


If you want to remove legends on all subplots, you can use the following code.

fig, axes = plt.subplots(2,5)

# ...

for ax in axes:
    ax.legend([],[], frameon=False)
like image 7
AndrejH Avatar answered Oct 07 '22 05:10

AndrejH