Search code examples
pythonseabornlinechart

Set lineplot marker for only one line


I am trying to plot a seaborn lineplot with 2 lines: one with markers and one without. In the documentation (https://seaborn.pydata.org/generated/seaborn.lineplot.html), it says that, in order to achieve this, I have to pass the the markers parameter as well as setting the style.

I tried 2 different approaches:

In the first one, I set the markers as True/False and pass a marker='o' to set the default marker. The problem with this approach is that it does not use the marker. It seems to use a '-' white marker as the default one.

fig, ax = plt.subplots(figsize=(7, 5))
sns.lineplot(data=my_data, x='Date', y='Value', markers={'Series 1': True, 'Series 2': False}, style='Name', marker = 'o')

In the second approach, I set the markers as "o" and None, but it raises a Value Error: Filled and line art markers cannot be mixed.

fig, ax = plt.subplots(figsize=(7, 5))
sns.lineplot(data=my_data, x='Date', y='Value', markers={'Series 1': 'o', 'Series 2': None}, style='Name')

What is the correct way to achieve the result I want?


Solution

  • You need to remember that seaborn is a wrapper on matplotlib, so if an error doesn't make sense or documentation seems incomplete, search the matplotlib documentation.

    I found that [',', '.', 'o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X'] (not an exhaustive list) works as input for the markers and ',' in particular will add a pixel marker which visually looks like no marker (unless you zoom in a lot). Hope this helps.

    See example below:

    import seaborn as sns
    flights = sns.load_dataset("flights")
    my_data = flights[flights['month'].isin(['Jan', 'Feb'])]
    sns.lineplot(data=my_data, x='year', y='passengers', style='month', 
        style_order=['Jan', 'Feb'], markers={'Jan': ',', 'Feb': 'o'}
    )
    

    enter image description here