0

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)

enter image description here

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!

bismo
  • 1,257
  • 1
  • 16
  • 36

1 Answers1

2

When I write code with the understanding that one line changes color, I draw a line from the data frame specifying lines 1 and 2, then lines 2 and 3. I end at the seventh of the index.

for idx, row in df.iterrows():
    #print(idx, row.value, row.colors)
    sns.lineplot([idx, idx+1], [row.value, df.loc[idx+1,'value']], color=row.colors)
    if idx == 7:
        break

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32