无法在seaborn distp中显示传奇

2024-04-19 08:06:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始用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()

Tags: testimportasnppltrandomseabornlabel
2条回答

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

有关matplotlib图例的更多信息,请参见documentation

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

相关问题 更多 >