作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在组装一个 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)
结果:
关于python - 有没有办法在 Bokeh 中使用 MultiSelect 来选择绘制哪个流数据通道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55644534/
我是一名优秀的程序员,十分优秀!