繁体   English   中英

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

[英]Is there a way to use a MultiSelect in Bokeh to choose which channel of streaming data is plotted?

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

我试过使用 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)

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

在此代码中,在MultiSelect选择一行会添加一个新行,如果它不在画布中并开始流式传输,或者如果该行已经在画布中,则只是切换流式传输。 代码适用于 Bokeh v1.0.4。 使用bokeh serve --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)

结果:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM