Hide legend from seaborn pairplot

左心房为你撑大大i 提交于 2020-02-23 09:37:27

问题


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)

回答1:


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()



来源:https://stackoverflow.com/questions/54781243/hide-legend-from-seaborn-pairplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!