Python 无法在seaborn distplot中显示图例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44968012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:41:52  来源:igfitidea点击:

Unable to show legend in seaborn distplot

pythonmatplotliblegendseaborn

提问by Rahul

I am new to plotting in python and trying following code to plot distribution in seabornbut unable to see the legend, i.e., test_label1and test_label1on the plot.

我是新来的蟒蛇策划并试图下面的代码情节分布seaborn,但无法看到传说,即test_label1test_label1对剧情。

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np

plt.figure("Test Plots")
lst1 = list(np.random.rand(10))
lst2 = list(np.random.rand(10))
sns.distplot(lst1, label='test_label1', color="0.25")
sns.distplot(lst2, label='test_label2', color="0.25")

plt.show()

回答by DavidG

As you have already labelled your plots using label=inside your sns.distplotthen all you have to do is show your legend. This is done by adding plt.legend()just before plt.show()

由于您已经使用label=inside标记了您的情节,sns.distplot那么您所要做的就是显示您的图例。这是通过plt.legend()在之前添加plt.show()

More information on matplotlib legends can be found in the documentation

有关 matplotlib 图例的更多信息可以在文档中找到

回答by Rubika Thirumoorthy

import seaborn as sns
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10,6))
lst1 = list(np.random.rand(10))
lst2 = list(np.random.rand(10))
sns.distplot(lst1)
sns.distplot(lst1)
fig.legend(labels=['test_label1','test_label2'])
plt.show()