Let's say I have the following df
df = pd.DataFrame(
{
'group':['a','a','a','b','b','a','a','c','c'],
'value':[10,5,9,3,6,4,8,1,10]
}
)
COLORS = {
'a':'blue',
'b':'red',
'c':'green'
}
df['colors'] = df.group.map(COLORS)
group value colors
0 a 10 blue
1 a 5 blue
2 a 9 blue
3 b 3 red
4 b 6 red
5 a 4 blue
6 a 8 blue
7 c 1 green
8 c 10 green
And I want to make a lineplot consisting of ONE LINE that changes color based on the group. (hue
will not help here, since we want the same line to change color based on group
, not a different line for each group
)
If you try to draw 3 different lines by group, they don't line up since the different groups are drawn individually
a = df.loc[df.group=='a']
b = df.loc[df.group=='b']
c = df.loc[df.group=='c']
sns.lineplot(a.index,a.value,data=a,color=COLORS[a.group.iloc[0]])
sns.lineplot(b.index,b.value,data=b,color=COLORS[b.group.iloc[0]])
sns.lineplot(c.index,c.value,data=c,color=COLORS[c.group.iloc[0]])
The same thing happens if you do the following
gk = df.groupby("group")
index = 0
for g, group in gk:
sns.lineplot(
x=group.index,
y=group['value'],
color=COLORS.get(g)
)
index += len(group)
This is not what I want. I want a continuous, single line plot that changes color based on group
(that is, I want the structure of the line to look just like it would if I had used a default value for color). I'm really stumped here!