Bokeh(0.12.1)使用散景服务以编程方式更新DataColumnSource选择(仅限Python)

时间:2016-08-15 11:10:25

标签: python bokeh

使用Bokeh,我试图通过Slider的回调以编程方式更新.selected ColumnDataSource字典,但无法设法在绘图中反映出选择。

在下面的片段中,我的想法是,我希望能够通过ybox_select工具和/或通过调整控制滑块的滑块来选择两者一对最小/最大线的位置(注意:为简洁起见,在本例中我只包括'max'滑块和线)。如果可能的话,我想在不使用CustomJS回调的情况下实现这一目标。

当我操作ybox_select工具(触发selection_change函数)时,我调整了水平线和滑块值(当然还有隐式选择)。相反,当我操作滑块(触发slider_selection功能)时,我设法控制水平线,但显然不是源选择。换句话说,在source.data中发生的slider_selection的修改反映在图中(即水平线的修改位置),但source.selected的修改未反映在图中(也不是在DataTable中,因为我单独验证)。

按照this thread中的建议(我已经问过这个问题的较短版本但到目前为止没有得到任何答案),我已经制作了source.selected的副本然后复制回.selected.data也一样),但这没有任何影响。

我必须遗漏一些相当基本的东西,但无法弄清楚是什么。任何的想法?请避免基于CustomJS的建议,除非您确定没有纯Python替代方案。

非常感谢任何反馈!

(注意:将此代码作为带有bokeh serve --show script.py的脚本运行)

from bokeh.io import curdoc
from bokeh.models import BoxSelectTool, Slider
from bokeh.plotting import figure, ColumnDataSource
from bokeh.sampledata.glucose import data
from bokeh.layouts import column
import numpy as np


#===============================================================================
# Data and source
y = data.ix['2010-10-06']['glucose']
x = np.arange(len(y))
maxval=[max(y)]*len(x)
source = ColumnDataSource(dict(x=x, y=y, maxval=maxval))

#===============================================================================
# Basic plot setup
tools = 'wheel_zoom,ybox_select,reset'
p = figure(plot_width=800, plot_height=400, tools=tools, title='Min/max selection')

# Plot data
cr = p.circle('x', 'y', color="blue", source = source,
              selection_color="blue", nonselection_color="gray", 
              size=6, alpha=0.8)

# Plot max horizontal line
p.line('x', 'maxval', line_color='blue', line_width=0.5, source=source,
       nonselection_alpha=1.0, nonselection_color='blue')

#===============================================================================
# Callbacks
def selection_change(attrname, old, new):
    ixs = new['1d']['indices']
    if ixs:
        arr = np.asarray(source.data['y'])[ixs]
        max_slider.value = np.max(arr)
        source.data['maxval'] = [np.max(arr)]*len(source.data['x'])

def slider_selection(attrname, old, new):
    selected = source.selected.copy()
    data = source.data.copy()
    data['maxval'] = [max_slider.value]*len(data['x'])
    yy = np.asarray(data['y'])
    maxi = np.asarray(data['maxval'])
    # Below is the new selection I would to visualize
    selected['1d']['indices'] = np.where(yy <= maxi)[0].tolist() 
    # Updated data is reflected in the plot (horizontal line at 'maxval' moves)
    source.data = data.copy()  
    # Updated selection is NOT reflected in the plot 
    # (nor in a DataTable, as tested separately)
    source.selected = selected.copy() 

#===============================================================================
# Slider
max_slider = Slider(start=min(y), end=max(y), 
                    value=max(y), step=0.1, title="Maximum")

#===============================================================================
# Trigger callbacks
source.on_change('selected', selection_change)
max_slider.on_change('value', slider_selection)

#===============================================================================
# Layout
plot_layout = column(p, max_slider)

curdoc().add_root(plot_layout)
curdoc().title = "Demo"

1 个答案:

答案 0 :(得分:0)

将以下行添加到slider_selection似乎可以执行您想要的操作:

def slider_selection(attrname, old, new):
    selected = source.selected.copy()
    data = source.data.copy()
    data['maxval'] = [max_slider.value]*len(data['x'])
    yy = np.asarray(data['y'])
    maxi = np.asarray(data['maxval'])
    # Below is the new selection I would to visualize
    selected['1d']['indices'] = np.where(yy <= maxi)[0].tolist()
    # Updated data is reflected in the plot (horizontal line at 'maxval' moves)
    source.data = data.copy()
    # Updated selection is NOT reflected in the plot
    # (nor in a DataTable, as tested separately)
    source.selected = selected.copy()
    source.trigger("selected", old, selected)

新功能定义:

{{1}}

(虽然现在有点迟了,我发现你的问题试图找到类似的答案,我认为这可能对其他人有用。)