我刚开始使用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()发布于 2017-07-07 18:58:12
由于您已经在sns.distplot中使用label=标记了地块,因此您所要做的就是显示图例。这是通过在plt.show()之前添加plt.legend()来完成的
有关matplotlib图例的更多信息,请参阅documentation
发布于 2019-05-06 17:35:07
通过使用fig.legend,我们可以在分布图中显示图例。在这里,labels的参数数组被传递给函数。图例中的标签也将显示为数组值的顺序。
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()发布于 2020-12-02 03:59:53
我发现一些问题,使用"fig.legend()“将labels添加到seaborn直方图中的legend中。它颠倒了图例中的颜色顺序,而不改变直方图条的颜色...
fig.legend(labels=['label1','label2',...])Histogram before using fig.legend()
Histogram after using fig.legend()
模块版本:
简单的解决方案是通过应用label_list.reverse()来颠倒label_list的顺序,但我不认为这是一个好的解决方案。我不知道发生了什么,以及如何阻止颠倒图例中颜色的顺序。
https://stackoverflow.com/questions/44968012
复制相似问题