【问题标题】:Bokeh: chart from pandas dataframe won't update on trigger散景:pandas 数据框的图表不会在触发时更新
【发布时间】:2016-04-08 10:26:39
【问题描述】:

我有一个 pandas 数据框,我想使用 Bokeh 服务器将其列显示为绘图中的线条。此外,我想要一个滑块,用于将其中一条线相对于另一条线移动。

我的问题是滑块值更改时的更新功能。我已经尝试了散景滑块示例中的代码,但它不起作用。

这是一个例子

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)

update_data-函数中,我必须更新mysource,但我不知道该怎么做。谁能指出我正确的方向?

【问题讨论】:

    标签: python pandas bokeh interaction


    【解决方案1】:

    试试这个...把a=offset.value改成a=cb_obj.get('value')

    然后将source.trigger('change') 放在update_data 函数中,而不是offset.on_change('value', update_data)

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

    请注意,我使用的这种格式适用于已安装的 flexxhttps://github.com/zoofio/flexx 如果您有 Python 3.5,则必须下载 zip 文件,解压缩并输入 python setup.py install,因为它尚未发布,尚未针对此版本编译...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多