Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using seaborn lineplot with grouping variable

I have a pandas DataFrame that looks like this.

      0      1      2     3      4      5     6     7     8     9  Group
0   0.0    0.0    0.0  12.5   12.5    0.0   0.0  12.5   0.0  12.5      1
1   0.0   12.5   12.5  12.5    0.0    0.0   0.0   0.0  12.5  12.5      1
2  37.5   37.5   37.5   0.0   37.5   37.5  25.0  25.0  37.5  25.0      1
3  25.0   50.0   25.0  25.0   50.0   50.0  25.0   0.0  37.5  50.0      1
4  50.0   62.5   50.0  62.5   50.0   50.0  62.5  50.0  62.5  50.0      1
0  12.5   12.5    0.0   0.0   12.5   12.5   0.0   0.0  12.5   0.0      2
1   0.0    0.0   12.5  12.5    0.0   12.5   0.0   0.0  25.0  25.0      2
2  50.0   25.0   37.5  12.5   37.5   25.0  37.5  25.0  37.5  37.5      2
3  25.0   50.0   25.0  12.5   37.5   37.5  25.0   0.0  37.5  50.0      2
4  62.5   50.0   50.0  62.5   50.0   50.0  62.5  50.0  62.5  50.0      2

Now i want too create a grouped lineplot with seaborn with two lines, one based on group 1 the other on group 2. The x-values are supposed to be the row-index (0,1,2,3,4) and the y-values should be the means+confidence intervals based on each row of my DataFrame.

My problem is that my dataformat is very different from what seaborn seems to require as input and I am not experienced with DataFrames so I do not know how to transform it correctly.

like image 339
arcGuesser Avatar asked Jul 12 '19 18:07

arcGuesser


1 Answers

I think this is what you want:

draw_df = df.reset_index().melt(id_vars=['index', 'Group'], var_name='col')

# turn to string
draw_df['Group'] = draw_df.Group.astype(str)

# pass custom palette:
sns.lineplot(x='index', 
             y='value',
             hue='Group', 
             palette=['b','r'],
             data=draw_df)

Output:

enter image description here

like image 156
Quang Hoang Avatar answered Oct 20 '22 23:10

Quang Hoang