无法在 seaborn distplot 中显示图例
Unable to show legend in seaborn distplot
我不熟悉在 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 图例的更多信息,请参阅 documentation
通过使用 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()
我发现使用“fig.legend()”将 labels 添加到 legend 在 seaborn 直方图中 。它反转图例中的颜色顺序而不改变直方图条的颜色...
fig.legend(labels=['label1','label2',...])
Histogram before using fig.legend()
Histogram after using fig.legend()
模块版本:
- seborn '0.11.0'
- matplotlib '3.1.3'
简单的解决方案是通过应用 label_list.reverse()
来颠倒 label_list
的顺序,但我认为这不是好的解决方案。我不知道发生了什么以及如何阻止颠倒图例中的颜色顺序。
我不熟悉在 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 图例的更多信息,请参阅 documentation
通过使用 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()
我发现使用“fig.legend()”将 labels 添加到 legend 在 seaborn 直方图中 。它反转图例中的颜色顺序而不改变直方图条的颜色...
fig.legend(labels=['label1','label2',...])
Histogram before using fig.legend()
Histogram after using fig.legend()
模块版本:
- seborn '0.11.0'
- matplotlib '3.1.3'
简单的解决方案是通过应用 label_list.reverse()
来颠倒 label_list
的顺序,但我认为这不是好的解决方案。我不知道发生了什么以及如何阻止颠倒图例中的颜色顺序。