【发布时间】:2018-11-05 18:04:03
【问题描述】:
从文档中,Selection 对象说:
选择通常是通过使用 SelectTool 在绘图中选择点来创建的,但也可以通过编程方式指定。
但我不知道如何以编程方式设置一些选定的点。例如,如果我想通过单击某个按钮来更新选择。我可以更新元素source.selected.indices,但是没有触发事件,点也没有标记为选中
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.layouts import column
from bokeh.models.tools import LassoSelectTool, TapTool
from bokeh.models.widgets.buttons import Button
source = ColumnDataSource(dict(
x=[1, 2, 3, 4, 5, 6],
y=[1, 2, 3, 4, 5, 6],
))
p = figure(
plot_height=300,
tools='',
)
p.circle( x='x', y='y', size=20, source=source)
lasso_select = LassoSelectTool(
select_every_mousemove=False,
)
tap = TapTool()
tools = (lasso_select, tap)
p.add_tools(*tools)
def update_selection_programmatically():
source.selected.update(indices=[4]) # the indices attribute is updated but the figure is not repainted, some event is not triggered.
# So the points are not marked as selected
bt = Button(
label="Update Selection",
button_type="success",
width=50
)
bt.on_click(update_selection_programmatically)
def update_selection(attr, old, new):
print('>> NEW SELECTION: {}'.format(new.indices))
# new.indices = [0] # this works fine here
source.on_change('selected', update_selection)
curdoc().add_root(column([p, bt]))
索引属性已更新,但图形未重新绘制。
【问题讨论】:
-
看起来像一个错误,我建议一个 GH 问题
标签: python python-3.x plot selection bokeh