python - 有没有办法在 Bokeh 中使用 MultiSelect 来选择绘制哪个流数据通道?

标签 python bokeh

我正在组装一个 Bokeh 服务器来收集多个数据流,并提供用户在多选菜单中选择的任何 channel 的实时绘图。我的流位正在工作,但我不确定如何选择在我添加到布局中的图中显示哪个流。

我尝试使用 curdoc().remove_root() 删除当前布局,然后添加一个新布局,但这只会杀死应用程序,并且新布局不会显示。我也尝试简单地更新数字,但这也只会杀死该应用程序。

from bokeh.layouts import column
from bokeh.plotting import figure,curdoc
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import MultiSelect

def change_plot(attr,old,new):
    global model,selector,p,source
    curdoc().remove_root(mode)
    p = figure()
    p.circle(x=new+'_x',y=new+'_y',source=source)
    model = column(selector,p)
    curdoc().add_root(model)

def update_plot():
    newdata = {}
    for i in range(10):
        # the following two lines would nominally provide real data
        newdata[str(i)+'_x'] = 1
        newdata[str(i)+'_y'] = 1
    source.stream(newdata,100)

selector = MultiSelect(title='Options',value=[str(i) for i in range(10)])
selector.on_change('value',change_plot)

data = {}
for i in range(10):
    data[str(i)+'_x'] = 0
    data[str(i)+'_y'] = 0
source = ColumnDataSource(data=data)

p = figure()
p.circle(x='0_x',y='0_y',source=source)
curdoc().add_root(model)
curdoc().add_periodic_callback(update_plot,100)

我使用 bokehserve --show app.py 运行此代码,我希望它每次更新 MultiSelect 时都会创建一个新绘图,但相反,它只是在change_plot回调中的某个地方崩溃了。

最佳答案

在此代码中,选择 MultiSelect 中的一行会添加一条新行(如果该行不在 Canvas 中),并开始流式传输,或者如果该行已在 Canvas 中,则仅切换流式传输。代码适用于 Bokeh v1.0.4。使用 bokehserve --show app.py

运行
from bokeh.models import ColumnDataSource, MultiSelect, Column
from bokeh.plotting import figure, curdoc
from datetime import datetime
from random import randint
from bokeh.palettes import Category10

lines = ['line_{}'.format(i) for i in range(10)]
data = [{'time':[], item:[]} for item in lines]
sources = [ColumnDataSource(item) for item in data]

plot = figure(plot_width = 1200, x_axis_type = 'datetime')

def add_line(attr, old, new):
    for line in new:
        if not plot.select_one({"name": line}):
            index = lines.index(line)
            plot.line(x = 'time', y = line, color = Category10[10][index], name = line, source = sources[index])

multiselect = MultiSelect(title = 'Options', options = [(i, i) for i in lines], value = [''])
multiselect.on_change('value', add_line)

def update():
    for line in lines:
        if line in multiselect.value:
            if plot.select({"name": line}):
                sources[lines.index(line)].stream(eval('dict(time = [datetime.now()], ' + line + ' = [randint(5, 10)])'))

curdoc().add_root(Column(plot, multiselect))
curdoc().add_periodic_callback(update, 1000)

结果:

enter image description here

关于python - 有没有办法在 Bokeh 中使用 MultiSelect 来选择绘制哪个流数据通道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55644534/

相关文章:

python - 如何在 Bokeh 中设置默认样式?

python - 从 Bokeh 中的 x 轴获取所需的日期时间值

python - 如何使用 Python 的 Noise 1.1.1 获取坐标?

python - 在较小表面内移动表面不会显示以前隐藏的组件

python - 如何使用 Bokeh 创建比例(预定义边界)颜色条?

python - 在 map Bokeh 上执行面积图时,它显示空白框并且不起作用

javascript - 通过 JavaScript 回调向数据表添加新列

python - 在命令提示符下运行 python :Access is Denied

python - Networkx 和 nx.write_gexf ... 动态节点属性

javascript - Jquery submit() 没有将表单数据提交到我的 View