如何在Bokeh应用程序中更新数据图

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

我有一个带有TimeSeries索引的Pandas DataFrame。我们的想法是使用散景服务器以获得DateSlider。这意味着我通过函数传递的值是日期。但是,我认为我的问题是当我运行散景服务器时数据没有更新。出现滑块但从滑块移动光标时数据未更新。我想有你的看法。这是我的代码:

#The file was already imported and cleaned as DF: trt_file 

day_one  = str(min(trt_file.index)) #Checking point file
day_last = str(max(trt_file.index)) #Checking point file

# Creating the data source which will be selected from the whole file

source = ColumnDataSource(data={
    "x"            : trt_file.loc[day_one]["Depth [m]"],
    "y"            : trt_file.loc[day_one]["Temperature"],
    "Temperature"  : trt_file.loc[day_one]["Temperature"],
    "Depth"        : trt_file.loc[day_one]["Depth [m]"]
})

#Minimum values for axis generation
xmin, xmax = min(trt_file["Depth [m]"]), max(trt_file["Depth [m]"])
ymin, ymax = min(trt_file["Temperature"]), max(trt_file["Temperature"])

#Set the figure fopr the initial draw
p = figure(title="2018-07-09", x_axis_label="Depth [m]", y_axis_label="Temperature", plot_height=300, plot_width=1500, x_range=(xmin, xmax), y_range=(ymin, ymax),tools=[HoverTool(tooltips=[("Temperature", "@Temperature"), ("Depth [m]", "@Depth")])]  )

#Draw the values from the source we defined first
p.circle(x="x", y="y", fill_alpha=0.5, source=source)

#Set the axis names
p.xaxis.axis_label = "Kable lenght [m]"
p.yaxis.axis_label = "Temperature [C]"

#Creating the DateTime for the Slider
day_one_dt = (min(trt_file.index)) #Checking point file
day_last_dt = (max(trt_file.index))

#Slider
slider = DateSlider(start=day_one, end=day_last, value=day_one, step=1, title="TRT Day")

#Creating the bokeh interactive figure
def update_plot(attr, old, new): #attribute to change, old_val, new_val
    #set day for the slider value
    day_start = (str(slider.value)) #Value from the slider
    new_data = {
        "x"          : trt_file.iloc[day_start]["Depth [m]"],
        "y"          : trt_file.iloc[day_start]["Temperature"],
        "Temperature": trt_file.iloc[day_start]["Temperature"],
        "Depth"      : trt_file.iloc[day_start]["Depth [m]"]
    }
    source.trt_file = new_data
    p.title.text = "Day TRT"

slider.on_change("value", update_plot)
layout = row(widgetbox(slider), p)


curdoc().title = "Temp"
curdoc().add_root(layout)
python pandas bokeh
1个回答
0
投票

Bokeh对“trt文件”一无所知,因此设置source.trt_file不是Bokeh会注意到或做任何事情的东西。 Bokeh知道并响应的属性是data

source.data = new_data
© www.soinside.com 2019 - 2024. All rights reserved.