使用stdev从数据框创建一个vbar,用于平均值和stdeviation

问题描述 投票:0回答:1

我有2个数据框,1个用于平均值和标准偏差,我试图将它们变成带有误差条的散景条形图但是我仍然坚持如何?? groupby ?? '设计'和治疗'。

基本上,我试图获得每个x值3个柱(T0到T2)。传说应该显示如下内容:'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)
pandas dataframe bokeh
1个回答
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函数完成错误栏

© www.soinside.com 2019 - 2024. All rights reserved.