在Bokeh中更新数据

时间:2018-11-25 23:12:28

标签: python bokeh

我是Bokeh的新手,不确定如何获取绘图数据以及如何更新数据。 我的代码遵循以下说明: https://github.com/WillKoehrsen/Bokeh-Python-Visualization/blob/master/interactive/exploration/interactive_development.ipynb

但是不幸的是,更新方法似乎不适用于我。我正在尝试查找有关此方法的文档,但找不到任何文档。有人可以协助吗? 基本上我已经从我的数据生成了一个熊猫数据框,并将其转换为ColumnDataSource。现在我想从中添加或减去数据,但是新的ColumnDataSource不会更新旧的。

任何帮助将不胜感激!到目前为止,这是我的代码,仍然无法正确更新:

def update(attr, old, new):
    stations_to_plot = [int(station_selection.labels[i]) for i in station_selection.active]
    by_station = pd.DataFrame(data=no_bikes.loc[stations_to_plot,:'23 PM'].values,index=list(map(str,initial_stations))
                          ,columns=no_bikes.loc[:,:'23 PM'].columns.tolist())
    new_src = ColumnDataSource(by_station.T)
    r.data_source.data.update(new_src.data)


stations=list(map(str,no_bikes.loc[:,'station_avg'].nlargest(10).index.tolist()))
station_selection = CheckboxGroup(labels=stations, active = [0,1,3])
station_selection.on_change('active', update)

initial_stations = [int(station_selection.labels[i]) for i in station_selection.active]   
range_select = RangeSlider(start = 0, end = 23, value = (0, 23),step = 1, title = 'Hours to plot')
range_select.on_change('value', update)

by_station = pd.DataFrame(data=no_bikes.loc[initial_stations,:'23 PM'].values,index=list(map(str,initial_stations))
                          ,columns=no_bikes.loc[:,:'23 PM'].columns.tolist())

src = ColumnDataSource(by_station.T)

p = figure(plot_width = 500, plot_height = 500, title = 'Chances not to find bikes',
                       x_axis_label = 'Hour of the day', y_axis_label = 'Proportion',x_range=src.data['index'])

for i,station in enumerate(src.data.keys()):
    if station in list(map(str, initial_stations)):
        r=p.line(x='index',y=station,source =src, legend='Station N.'+station, color=Category20_16[i], line_width=5)




controls = WidgetBox(station_selection,range_select)
layout = row(controls, p)
curdoc().add_root(layout)

0 个答案:

没有答案