简体   繁体   English

无法在seaborn distplot中显示图例

[英]Unable to show legend in seaborn distplot

I am new to plotting in python and trying following code to plot distribution in seaborn but unable to see the legend, ie, test_label1 and test_label1 on the plot.我是在 python 中绘图的新手,并尝试使用以下代码绘制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()

As you have already labelled your plots using label= inside your sns.distplot then all you have to do is show your legend.由于您已经在sns.distplot使用label=标记了您的图,那么您所要做的就是显示您的图例。 This is done by adding plt.legend() just before plt.show()这是通过在plt.show()之前添加plt.legend()来完成的

More information on matplotlib legends can be found in the documentation有关 matplotlib 图例的更多信息可以在文档中找到

By using fig.legend we can show legends in the distribution plot.通过使用fig.legend我们可以在分布图中显示图例。 Here, an argument array of labels is passed to the function.在这里, labels的参数数组被传递给函数。 Labels in the legend will also be displayed as an order of array values.图例中的标签也将显示为数组值的顺序。

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

I found some issue using " fig.legend() " to adding labels into legend in seaborn histogram .我发现使用“ fig.legend() ”将标签添加到seaborn histogram 中的图例出现了一些问题。 It reverse colors order in legend without changing colors of histogram bars...它反转图例中的颜色顺序而不改变直方图条的颜色......

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

Histogram before using fig.legend()使用 fig.legend() 之前的直方图

Histogram after using fig.legend()使用 fig.legend() 后的直方图

Modules versions:模块版本:

  • seborn '0.11.0'塞伯恩'0.11.0'
  • matplotlib '3.1.3' matplotlib '3.1.3'

Simple solution is to reverse the order of label_list by appling label_list.reverse() but I don't consider it as good solution.简单的解决方案是通过应用label_list.reverse()来反转label_list的顺序,但我认为它不是一个好的解决方案。 I don't have idea what is happening and how to block reversing the order of colors in legend.我不知道发生了什么以及如何阻止反转图例中的颜色顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM