Bokeh: chart from pandas dataframe won't update on trigger

谁说我不能喝 提交于 2019-12-25 04:38:23

问题


I have got a pandas dataframe whose columns I want to show as lines in a plot using a Bokeh server. Additionally, I would like to have a slider for shifting one of the lines against the other.

My problem is the update functionality when the slider value changes. I have tried the code from the sliders-example of bokeh, but it does not work.

Here is an example

import pandas as pd
from bokeh.io import vform
from bokeh.plotting import Figure, output_file, show
from bokeh.models import CustomJS, ColumnDataSource, Slider

df = pd.DataFrame([[1,2,3],[3,4,5]])
df = df.transpose()
myindex = list(df.index.values)
mysource = ColumnDataSource(df)

plot = Figure(plot_width=400, plot_height=400)

for i in range(len(mysource.column_names) - 1):
    name = mysource.column_names[i]    
    plot.line(x = myindex, y = str(name), source = mysource)

offset = Slider(title="offset", value=0.0, start=-1.0, end=1.0, step=1)

def update_data(attrname, old, new):
    # Get the current slider values
    a = offset.value

    temp = df[1].shift(a)
    #to finish#

offset.on_change('value', update_data)

layout = vform(offset, plot)

show(layout)

Inside the update_data-function I have to update mysource, but I cannot figure out how to do that. Can anybody point me in the right direction?


回答1:


Give this a try... change a=offset.value to a=cb_obj.get('value')

Then put source.trigger('change') after you do whatever it is you are trying to do in that update_data function instead of offset.on_change('value', update_data).

Also change offset = Slider(title="offset", value=0.0, start=-1.0, end=1.0, step=1, callback=CustomJS.from_py_func(offset))

Note this format I'm using works with flexx installed. https://github.com/zoofio/flexx if you have Python 3.5 you'll have to download the zip file, extract, and type python setup.py install as it isn't posted yet compiled for this version...



来源:https://stackoverflow.com/questions/36497285/bokeh-chart-from-pandas-dataframe-wont-update-on-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!