散景中的自定义 JavaScript 回调,用于从数据帧中选择列并更新绘图



我有一个 excel 表,其中包含 20 多个元素(每个元素都是一列(随时间推移(运行(的浓度。

我必须绘制每个元素的平均值和标准偏差上的浓度与时间的关系图。

我想制作一个散景程序,让我选择要查看的元素并相应地更新情节。我不想为了它工作而必须连接到服务器,我希望它是一个独立的 html。所以,我知道我需要编写一个自定义的 js 回调来做到这一点。

我的代码现在的工作方式是,我有一个函数来计算均值和标准差,将其存储在新的 df 中,并使用该 df 制作绘图。

这是我到目前为止的代码。现在它以一种编码的方式,以便它只显示一个元素

import pandas as pd
import os
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, HoverTool,CustomJS
from bokeh.models.widgets import Select
from bokeh.layouts import row, column
def get_data(low,element):
mean=low[element].mean()
plus_three_sigma=mean+(low[element].std()*3)
minus_three_sigma=mean-(low[element].std()*3)
plus_two_sigma=mean+(low[element].std()*2)
minus_two_sigma=mean-(low[element].std()*2)
df=pd.DataFrame({"Run":low["run"],element:low[element],"mean":mean,"plus_three_sigma":plus_three_sigma,"minus_three_sigma":minus_three_sigma,"plus_two_sigma":plus_two_sigma,"minus_two_sigma":minus_two_sigma})
return df
def make_plot(df):
tips=[("Run", "@Run"),("Concentration", "$y")]
source=ColumnDataSource(df)
p = figure(plot_width=1300, plot_height=800, x_range=df["Run"], tooltips=tips, title="QC Low", x_axis_label="Run ID",y_axis_label="Concentration ng/mL")
p.line(x="Run", y="mean", line_width=1, color="black", source=source)
p.line(x="Run", y="plus_three_sigma", line_width=1, color="red", source=source)
p.line(x="Run", y="minus_three_sigma", line_width=1, color="red", source=source)
p.line(x="Run", y="minus_two_sigma", line_width=1, color="green",line_dash="dashed",source=source)
p.line(x="Run", y="plus_two_sigma", line_width=1, color="green",line_dash="dashed",source=source)
pc=p.circle(x='Run', y="9Be",source=source)
p.xaxis.major_label_orientation = 1.2
return p
#callback = CustomJS(args=, code="""
#
#    }
#    source.change.emit();
#""")
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    
os.chdir(r'')

low=pd.read_excel(r"", sheet_name="QC LOW", skiprows=5, usecols=range(0,34))
low["run"]=low["run"].astype(str)
low.loc[~(low["run"].str.contains("A")) & ~(low["run"].str.contains("B")),"run"]=pd.to_datetime(low.loc[(~low["run"].str.contains("A")) & (~low["run"].str.contains("B")),"run"]).dt.strftime('%m/%d/%y')
cols=low.columns.tolist()
cols=cols[2:]
select = Select(title="Option:", value="9Be", options=cols)
output_file("output.html")
df=get_data(low,"9Be")
p=make_plot(df)
#select.js_on_change('value', callback)
show(row(select,p))

我不明白如何编写 javascript 回调来更新数据和绘图。我是否需要将get_data移动到 java 脚本回调中?我不应该在函数中制作图形吗?我将如何编写JavaScript来完成我想要的。

所以有几件事需要改变,而且有很多方法可以做到这一点,但这就是我一直让它工作的方式。

1(您的df应该是一个字典,在您的情况下由不同的元素分组。像这样:

df2 = df.groupby('Element', sort = False).apply(lambda x: x.to_dict(orient = 'list'))

2(您的columndatasource将仅指向该字典中的第一个键。

source = ColumnDataSource(data = df2[0])

3(您的选择值应该是df2的键(换句话说,每个元素(

opts = [*df2.keys()]
select = Select(value = opts[0], options = opts)

4(你的JS将看起来像这样:

callback = CustomJS(args = dict(graph=source, source= df2.to_dict()), code =
"""                
graph.data = source[cb_obj.value];
graph.change.emit();
""")

Graph 是你的列数据源,Graph.data 是你的第一个元素数据。您的cb_obj.value将是您在选择时选择的选项,并将充当源(df2.to_dict(的键,以提取正确选择的数据。换句话说,当您在"选择"下拉列表中选择一个元素时,您将用新选择的元素交换数据。

终于让它显示:

select.js_on_change('value', callback)
layout = column(select, p)
show(layout)

最新更新