在bokeh中更新我的滑块时没有任何变化

时间:2019-04-02 14:23:38

标签: python-3.x bokeh

我想绘制%commit如何随着时间的推移而改变日期。这意味着将时间用作x,将%commit用作y。但是当我拖动滑块时,什么都没有改变。

我尝试了回调函数和CustomJs。数据信息是:

索引:4289个条目,从2018-11-01到2018-10-31

enter image description here

索引是字符串日期yyyy-mm-dd。

from bokeh.layouts import gridplot
from bokeh.models import DateSlider
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider,DatetimeTickFormatter, HoverTool
source = ColumnDataSource(data={
    'x'       : temp.loc['2018-11-01']["timeStamp"],
    'y'       : temp.loc['2018-11-01']["%commit"]
})



plot_options = dict(width=700, plot_height=400,tools=[hover])

slider = DateSlider(start=min(temp.index), end=max(temp.index), value=min(temp.index), step=1, title="Date")
p = figure(**plot_options)
p.line(x='x', y='y', source = source)
p.xaxis.formatter = DatetimeTickFormatter(days="%d-%b-%Y", hours="%H:%M", seconds="%S" )

print(temp.loc[str(slider.value)].head())

def update_plot(attr, old, new):
    date = new
    print(date)
    new_data = ColumnDataSource(data={
    'x'       : temp.loc[str(date)]["timeStamp"],
    'y'       : temp.loc[str(date)]["%commit"]
})
    source.data = new_data

slider.on_change('value', update_plot)

# the part above is CustomJS solution, also doesn't work
# update_curve = CustomJS(args=dict(source=source, slider=slider), code="""
#     var data = source.data;
#     var f = str(slider.value).replace('-','/');
#     x = data['x']
#     y = data['y']
#     for (i = 0; i < y.length; i++) {
#         y[i] = Math.pow(x[i], f)
#     }
#     // necessary becasue we mutated source.data in-place
#     source.trigger('change');
# """)

# slider.js_on_change('value', update_curve)


# show the results
show(column(slider, p))

1 个答案:

答案 0 :(得分:0)

source.data是一本字典,所以您应该这样做:

def update_plot(attr, old, new):
    date = new
    new_data = {'x': temp.loc[str(date)]["timeStamp"],
                'y': temp.loc[str(date)]["%commit"] }
    source.data = new_data