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)
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.
Example 1: By using ax. legend_ = None, legend can be removed from figure in matplotlib.
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.
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.
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.
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.
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.
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.
Since _legend.remove()
method won't work on some other seaborn plots, what about:
plt.legend([],[], frameon=False)
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()
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With