seaborn lineplot:图例中缺少标记符号

问题描述

我有一个带有标记符号“o”的seaborn线图(所有系列都应使用相同的符号)。适用于图表,但图例中缺少标记符号(请参见屏幕截图)。我怎样才能让它出现?

代码

import seaborn as sns
fmri = sns.load_dataset("fmri")
sns.lineplot(data=fmri,x="timepoint",y="signal",hue="event",marker="o")

输出

enter image description here

解决方法

评论者提供的评论都解决了我的问题,但结果略有不同。为了其他用户的利益,下面说明了这些差异。

解决方案 1: 此处,每个系列的标记在图例中出现两次。

代码:

import seaborn as sns
fmri = sns.load_dataset("fmri")
ax=sns.lineplot(data=fmri,x="timepoint",y="signal",hue="event",marker="o")
for h in ax.legend_.legendHandles: 
    h.set_marker('o')

输出:

enter image description here

解决方案 2a: 在这里,每个系列的标记在图例中出现一次。每个系列的线条样式都不同。

代码:

import seaborn as sns
fmri = sns.load_dataset("fmri")
sns.lineplot(data=fmri,style="event",markers=["o"]*2)

输出:

enter image description here

解决方案 2b: 非常像解决方案 2a,但为了使所有行都为“实线”样式,添加了以下选项:

dashes=[""]*2

代码:

import seaborn as sns
fmri = sns.load_dataset("fmri")
sns.lineplot(data=fmri,markers=["o"]*2,dashes=[""]*2)

输出:

enter image description here