TYL
TYL

Reputation: 1637

Seaborn multi line plot with only one line colored

I am trying to plot a multi line plot using sns but only keeping the US line in red while the other countries are in grey

This is what I have so far:

df = px.data.gapminder()
sns.lineplot(x = 'year', y = 'pop', data = df, hue = 'country', color = 'grey', dashes = False, legend = False)

But this does not change the lines to grey. I was thinking that after this, I could add in US line by itself in red.....

Upvotes: 2

Views: 7625

Answers (3)

Quang Hoang
Quang Hoang

Reputation: 150745

You can use pandas groupby to plot:

fig,ax=plt.subplots()
for c,d in df.groupby('country'):
    color = 'red' if c=='US' else 'grey'
    d.plot(x='year',y='pop', ax=ax, color=color)

ax.legend().remove()

output:

enter image description here

To keep the original colors for a default palette, but grey out the rest, you can choose to pass color='grey' only when the condition is met:

if c in some_list:
    d.plot(...)
    else:
        d.plot(..., color='grey')

Or you can define a specific palette as a dictionary:

palette = {c:'red' if c=='US' else 'grey' for c in df.country.unique()}

sns.lineplot(x='year', y='pop', data=df, hue='country', 
             palette=palette, legend=False)

Output:

enter image description here

Upvotes: 7

jumbofiatco
jumbofiatco

Reputation: 21

Easily scalable solution:

  1. Split dataframe into two based on lines to be highlighted

    lines_to_highlight = ['USA'] hue_column = 'country'

a. Get data to be grayed out

df_gray = df.loc[~df[hue_column].isin(lines_to_highlight)].reset_index(drop=True)

Generate custom color pallet for grayed out lines - gray hex code #808080

gray_palette = {val:'#808080' for val in df_gray[hue_column].values}

b. Get data to be highlighted

df_highlight = df.loc[df[hue_column].isin(lines_to_highlight)].reset_index(drop=True)
  1. Plot the two data frames on the same figure

a. Plot grayed out data:

ax = sns.lineplot(data=df_gray,x='year',y='pop',hue=hue_column,palette=gray_palette)

b. Plot highlighted data

sns.lineplot(data=df_highlight,x='year',y='pop',hue=hue_column,ax=ax)

Upvotes: 0

Arne
Arne

Reputation: 10545

You can use the palette parameter to pass custom colors for the lines to sns.lineplot, for example:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({'year': [2018, 2019, 2020, 2018, 2019, 2020, 2018, 2019, 2020, ], 
                   'pop': [325, 328, 332, 125, 127, 132, 36, 37, 38], 
                   'country': ['USA', 'USA', 'USA', 'Mexico', 'Mexico', 'Mexico',
                               'Canada', 'Canada', 'Canada']})

colors = ['red', 'grey', 'grey']
sns.lineplot(x='year', y='pop', data=df, hue='country', 
             palette=colors, legend=False)

plt.ylim(0, 350)
plt.xticks([2018, 2019, 2020]);

red-gray lineplot

It could still be useful to have a legend though, so you may also want to consider tinkering with the alpha values (the last values in the tuples below) to highlight the USA.

red = (1, 0, 0, 1)
green = (0, 0.5, 0, 0.2)
blue = (0, 0, 1, 0.2)
colors = [red, green, blue]

sns.lineplot(x='year', y='pop', data=df, hue='country', 
             palette=colors)

plt.ylim(0, 350)
plt.xticks([2018, 2019, 2020]);

various alpha example plot

Upvotes: 1

Related Questions