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.....
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:
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: