从数据框中使用stdev制作vbar以获取均值和标准差

时间:2018-09-06 14:45:12

标签: pandas dataframe bokeh

我有2个数据框,其中1个用于均值和标准差,我正试图将它们变成散景图,带有误差条,但我一直坚持如何分组? “设计”和“处理”。

基本上,我试图每个x值(T0至T2)获得3条。图例应显示类似:“ mouse-yes”,“ mouse-no”和“ cat-no”。如何重组dict或数据框以将其转换为vbar?然后如何将其与stdev数据框结合在一起?

还有没有办法使图中的x_range自动获取“时间”列中的所有原始值?我希望能够将x轴的“时间”,“设计”和“处理”列互换。我猜这是方便使用数据透视表的地方。

from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import dodge


output_file("dodged_bars.html")

import pandas as pd
dat_mean=[['T0','mouse','yes',25],['T0','mouse','no',24],['T0','cat','no',23],['T1','mouse','yes',15],['T1','mouse','no',14],['T1','cat','no',13],['T2','mouse','yes',5],['T2','mouse','no',4],['T2','cat','no',3]]
df_mean= pd.DataFrame(dat_mean,columns = ["Time", "Design", "Treatment", "Mean for Cmpd1"])

dat_std=[['T0','mouse','yes',5],['T0','mouse','no',5],['T0','cat','no',5],['T1','mouse','yes',2.5],['T1','mouse','no',2.5],['T1','cat','no',2.5],['T2','mouse','yes',1],['T2','mouse','no',1],['T2','cat','no',1]]
df_std= pd.DataFrame(dat_std,columns = ["Time", "Design", "Treatment", "Mean for Cmpd1"])



data = df_mean.to_dict(orient='list')*

dates = df_mean['Time'].tolist()

source = ColumnDataSource(data=data)

p = figure(x_range=['T0', 'T1', 'T2'], y_range=(0, 30), plot_height=250, title="Bokeh plot",
           toolbar_location=None, tools="")



p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

show(p)

1 个答案:

答案 0 :(得分:0)

from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import dodge


output_file("dodged_bars.html")

import pandas as pd
dat_mean=[['T0','mouse','yes',25],['T0','mouse','no',24],['T0','cat','no',23],['T1','mouse','yes',15],['T1','mouse','no',14],['T1','cat','no',13],['T2','mouse','yes',5],['T2','mouse','no',4],['T2','cat','no',3]]
df_mean= pd.DataFrame(dat_mean,columns = ["Time", "Design", "Treatment", "Mean for Cmpd1"])

dat_std=[['T0','mouse','yes',5],['T0','mouse','no',5],['T0','cat','no',5],['T1','mouse','yes',2.5],['T1','mouse','no',2.5],['T1','cat','no',2.5],['T2','mouse','yes',1],['T2','mouse','no',1],['T2','cat','no',1]]
df_std= pd.DataFrame(dat_std,columns = ["Time", "Design", "Treatment", "Mean for Cmpd1"])

time_seq=df_mean['Time'].drop_duplicates()
time_vals=time_seq.tolist()
MEANs=df_mean.groupby(["Design", "Treatment"])["Mean for Cmpd1"].apply(list).to_dict()

keys=[]
for h in range(len(MEANs)):
    raw_key=list(MEANs.keys())[h]
    keys.append(raw_key[0]+'_'+raw_key[1])

results = {'time_vals' : time_vals,
        keys[0]   : list(MEANs.values())[0],
        keys[1]   : list(MEANs.values())[1],
        keys[2]   : list(MEANs.values())[2]}

source = ColumnDataSource(data=results)

p = figure(x_range=['T0', 'T1', 'T2'], y_range=(0, 30), plot_height=250, title="Bokeh plot",
           toolbar_location=None, tools="")
for hh in range(len(MEANs)):
    p.vbar(x=dodge('time_vals', -0.25+.2*hh, range=p.x_range), top=keys[hh], width=0.2, 
       source=source,color=color[hh], legend=value(keys[hh]))


p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

show(p)

此代码有效。可以使用add_layout和Whisker函数完成错误栏