基于多索引 pandas 数据帧使用 matplotlib 绘制错误栏

plot errorbar with matplotlib based on multiindex pandas dataframe

我在 pandas 中有以下数据框:

>>>name   Hour   trt_level    stress   date          value
0  D43    9         H         control  2019-06-07    0.4561
1  D43    10        H         control  2019-06-07    0.3216
2  D42    8         M         stress   2019-06-07    0.2143
3  D42    9         M         stress   2019-06-07    0.1342
4  D21    8         L         stress   2019-06-07    0.3214
...

我想创建带误差线的折线图,mse/std,看起来像这样:

from : https://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.htmlbut 在我的例子中:X 轴应该是小时,y 轴应该是值,三行,每个治疗级别一条(trt_level)所以行H,M,L.

为了做到这一点,我使用了函数 groupby 和 agg :

data = df.groupby(['trt_level','Hour']).agg([np.mean, np.std])
data.head()

>>>                value
                   mean      std
trt_level  Hour   
H           7      0.231      0.0058
            8      0.212      0.0094
            9      0.431      0.1154
...


wwhich gave eme database with the treamtnet and hour as index and mean and std of the value, 但问题是,当我尝试绘制它时,我只得到一行,上面没有 std:

data = data['value'] 
qual.plot(kind = "line", y = "mean", legend = False,  
          xerr = "std", title = "test", color='green')

当我想要的结果应该有三行,std 在上面时(如果可以是 MES 而不是 std 更好,但是对于这个问题,我更关注三行和 std 的显示)

我的最终目标是获得更像这样的图表(很抱歉画得太糟糕了):

但所有时间

快到了。您必须拆开您的 multi-index 数据框。

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

#My test file contained at least two values per condition to calculate an SD value
#df = pd.read_csv("test.txt", sep = "\s{2,}") 

dfm = df.groupby(["trt_level","Hour"]).agg([np.mean, np.std])

dfm["value"].unstack(level=0).plot(y = "mean", yerr = "std", title = "TRT levels are really important!", color = list("rbg"))

plt.show()

示例输出

顺便说一句:kind="line" 不必指定,它是默认值。 pandas 文档列出了所有 possible keywords for kind.