plot errorbar with matplotlib based on multiindex pandas dataframe

左心房为你撑大大i 提交于 2021-01-28 05:17:47

问题


I have the following dataframe in 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
...

I want to create line chart with error-bar,with mse/std, something that will look like this:

from : https://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.htmlbut in my case: the X-axis should be hour, the y axis the values, and three lines, one for each level of treatment (trt_level) so line for H,M,L.

In order to do that I have used function groupby and 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 gav eme database with the treamtnet and hour as index and mean and std of the value, but the problem is that when I try to plot it I get only one line without the std on top:

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

When my desired result should have three lines with the std on top (better if could be MES and not std but for this question I focus more on the three lines and the displaying of the std)

My end goal is to get chart that is more like this (sorry for the horrible draw):

but for all the hours


回答1:


Nearly there. You have to unstack your multi-index dataframe.

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()

Sample output

BTW: kind="line" does not have to be specified, it is the default. The pandas documentation lists all possible keywords for kind.



来源:https://stackoverflow.com/questions/64550318/plot-errorbar-with-matplotlib-based-on-multiindex-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!