Python 无法在seaborn distplot中显示图例

Python 无法在seaborn distplot中显示图例,python,matplotlib,legend,seaborn,Python,Matplotlib,Legend,Seaborn,我不熟悉python绘图,并尝试使用下面的代码在seaborn中绘制分布,但无法在绘图上看到图例,即test_label1和test_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(l

我不熟悉python绘图,并尝试使用下面的代码在
seaborn
中绘制分布,但无法在绘图上看到图例,即
test_label1
test_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()

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

有关matplotlib图例的更多信息,请使用查看分布图中的图例。 这里,
标签
的参数数组被传递给函数。 图例中的标签也将按数组值的顺序显示

导入seaborn作为sns
将matplotlib.pyplot作为plt导入
图=plt.图(图尺寸=(10,6))
lst1=列表(np.random.rand(10))
lst2=列表(np.random.rand(10))
sns.distplot(lst1)
sns.distplot(lst1)
图图例(标签=['test_label1','test_label2'])
plt.show()
我使用“fig.legend()”在seaborn直方图中的图例中添加标签,发现了一些问题。它可以反转图例中的颜色顺序,而不更改直方图条的颜色

fig.legend(labels=['label1','label2',...])

模块版本:

  • 塞伯恩'0.11.0'
  • matplotlib“3.1.3”

简单的解决方法是通过使用<代码> LabelSyList.RealSee()/代码>来反转<代码> LabelListList<代码>,但我不认为它是一个好的解决方案。我不知道发生了什么,也不知道如何阻止在图例中反转颜色顺序。

plt.legend()
?谢谢@DavidG。这是可行的,但唯一的问题是,我必须在最后单独做。所以像
plt.legend(['test\u label1','test\u label2'])
这样的东西需要记住顺序。您不必这样做,因为您已经在绘图中指定了
label=
。在
plt.show()
之前调用
plt.legend()
会起作用(对我来说是这样)这真的是“Seaborn方式”,直接使用matplotlib创建图例?@matanster我真的看不到任何问题,因为Seaborn本质上只是matplotlib的包装器。有一些方法可以让图例只使用
sns.distplot
的参数来显示,但是,在我看来,这并不简单,只有答案被认为是低质量的帖子,你的答案并添加一些解释