Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 添加自定义图例_Python_Matplotlib_Seaborn_Legend - Fatal编程技术网

Python 添加自定义图例

Python 添加自定义图例,python,matplotlib,seaborn,legend,Python,Matplotlib,Seaborn,Legend,如何在seaborn图表上添加自定义图例?我不熟悉seaborn和matplotlib,带有手柄和标签的自定义图例非常混乱 for target in targets: sns.distplot(target[['sepal length (cm)']], hist=False, rug=True, label = target) plt.legend(unique_vals) sns.distplot(target[['sepal length (cm)']==1], his

如何在seaborn图表上添加自定义图例?我不熟悉seaborn和matplotlib,带有手柄和标签的自定义图例非常混乱

for target in targets:
    sns.distplot(target[['sepal length (cm)']], hist=False, rug=True, label = target)
    plt.legend(unique_vals)

sns.distplot(target[['sepal length (cm)']==1], hist=False, rug=True)

我已经添加了上面的函数来创建图表和标签。然后我又添加了一个分布图。如何为第二个分布图添加标签?

我认为您需要提供有关程序和数据的更多详细信息,因为下面的示例正确地绘制了图例,即使没有
图图例()

导入matplotlib.pyplot作为plt
导入seaborn作为sns
将numpy作为np导入
目标=[]
对于范围(3)中的i:
目标=np.rand.rand(10)
targets.append(目标)
图,ax=plt.子批次()
对于i,枚举中的目标(目标):
sns.distplot(target,hist=False,rug=True,label=i)
sns.distplot([i为目标[0]中的i,如果i>0.3],hist=False,rug=True,label=len(目标))
#图图例()
plt.show()

除了用标签配置每个绘图外,您是否尝试过在完成所有绘图后使用
plt.legend()
?是的,我尝试过。它所做的是,它将列中的值列表作为图例。最终,plt.annotate帮助我创建了一个图例,而我仍在努力创建一个图例。您将列中的值列表作为图例是因为您将标签定义为
label=target
label
将用作绘图图例。尝试将
标签
与字符串值关联。第一个并没有给我带来问题,但当我在第二个绘图中使用标签时,即使是作为字符串,它也会给我列中的值列表。