Issue
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.....
Solution
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:
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:
Answered By - Quang Hoang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.