简体   繁体   中英

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.

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM