2

I have a seaborn lineplot with marker symbol "o" (the same symbol should be used for all series). Works well for the chart, but the marker symbol is missing in the legend (see screenshot). How can I make it appear?

Code:

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

Output:

enter image description here

Barden
  • 1,020
  • 1
  • 10
  • 17
  • I'm not sure why the markers don't appear automatically. But you can call `for h in ax.legend_.legendHandles: h.set_marker('o')` where `ax = sns.lineplot(...)` to update the legend afterwards. – JohanC Jul 14 '21 at 20:47
  • The recommended way to get markers in a `lineplot` is via the `markers=` keyword which needs to go together with `style=`. E.g. `sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event", style="event", markers=["o"]*2)` or `sns.lineplot(..., style="event", markers=True)` – JohanC Jul 14 '21 at 23:02
  • Both comments are viable solutions, @JohanC, as my modified post illustrates. If you enter one (the second with "markers" preferred) as solution, I'll gladly accept it. Thx! – Barden Jul 15 '21 at 11:12
  • You might post your two solutions as an answer, and then auto accept them. – JohanC Jul 15 '21 at 12:13
  • ok, will do, just did not want to take credit from you. Thx for your help. – Barden Jul 15 '21 at 12:16

1 Answers1

1

The comments provided by the commenter both solved my problem but with slightly different results. For the benefit of other users these differences are illustrated below.

Solution 1: Here the markers appear in the legend twice for each series.

Code:

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

Output:

enter image description here

Solution 2a: Here the markers appear in the legend once for each series. The line style is different for each series.

Code:

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

Output:

enter image description here

Solution 2b: Very much like solution 2a, but in order to have all lines in "solid" style the following option is added:

dashes=[""]*2

Code:

import seaborn as sns
fmri = sns.load_dataset("fmri")
sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event", style="event", markers=["o"]*2, dashes=[""]*2)

Output:

enter image description here

Barden
  • 1,020
  • 1
  • 10
  • 17