python - 将 seaborn lineplot 与分组变量一起使用

标签 python pandas dataframe seaborn confidence-interval

我有一个看起来像这样的 pandas DataFrame。

      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

现在我也想创建一个 grouped lineplot seaborn 有两条线,一条基于第 1 组,另一条基于第 2 组。x 值应该是行索引 (0,1,2,3,4),y 值应该是平均值+基于我的 DataFrame 的每一行的置信区间。

我的问题是我的数据格式与 seaborn 似乎需要的输入格式非常不同,而且我没有使用 DataFrame 的经验,所以我不知道如何正确转换它。

最佳答案

我想这就是你想要的:

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)

输出:

enter image description here

关于python - 将 seaborn lineplot 与分组变量一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57012286/

相关文章:

python - 如何在找到值后立即在字符串搜索中跳到索引? Python

python - 水平解析表

python - 如何使用 pandas 从 csv 读取多行到单个数据帧行

python - 在seaborn中绘制单独的组时如何将数据作为一组包含

python - 替换列并扩展 DataFrame 中的索引

Python - 将一个函数传递给另一个函数

python - re.findall 行为怪异

python - 在屏幕上显示各种 Pandas 数据框

python - aggfunc 中具有不同条件的 Pandas 数据透视表

python - Conda ResolvePackageNotFound 错误