Search code examples
matplotlibseabornlegend-properties

seaborn lineplot: marker symbols missing on legend


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


Solution

  • 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