avi*_*iss 7 matplotlib python-3.x seaborn
我想隐藏Seaborn对偶图例。官方文档没有提及关键字图例。我尝试使用的所有方法plt.legend
均无效。请提出最佳的前进方式。谢谢!
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)
Run Code Online (Sandbox Code Playgroud)
小智 29
由于_legend.remove()
方法不适用于其他一些seaborn图,那么:
plt.legend([],[], frameon=False)
Run Code Online (Sandbox Code Playgroud)
And*_*ner 10
如果要删除所有子图上的图例,可以使用以下代码。
fig, axes = plt.subplots(2,5)
# ...
for ax in axes:
ax.legend([],[], frameon=False)
Run Code Online (Sandbox Code Playgroud)
使用时需要返回Seabron Pairgrid对象pairplot
,然后可以使用来访问Pairgrid的图例._legend
。然后只需调用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()
Run Code Online (Sandbox Code Playgroud)
You can also just stay on the surface roads, so to speak.
ax.get_legend().set_visible(False)
Run Code Online (Sandbox Code Playgroud)