Seaborn的distplot中标签未显示

3
我正在使用Seaborn的distplot()函数,将两个密度图并排绘制在同一张图上,并希望给它们加上标签。
我使用了函数文档中提到的'label'参数。
代码如下:
Response4_mask = train_with_response['Response4'] == 1
not_Response4_mask = train_with_response['Response4'] != 1

plt.figure()

sns.distplot(a = train_imp_with_response[Response4_mask]['Family_Hist_4'], hist = True, color = 'red', label = 'Response4')
sns.distplot(a = train_imp_with_response[not_Response4_mask]['Family_Hist_4'], hist = True, label = 'not_Response4')

plt.title('Family_Hist_4')

plt.tight_layout()
plt.show()

以下是输出内容,其中没有标签:

这里输入图片描述

1个回答

6

只需添加

plt.legend()

查看legend()函数的文档和matplotlib的图例指南以获取更多细节。

plt.show()之前执行以上操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接