如何制作交互式散景图?

时间:2017-07-19 17:12:59

标签: python bokeh

我第一次尝试散景,在网络浏览器上制作互动情节。我绘制了一些圆圈,并且当我点击其中一个圆圈时想要一些互动(回调)。我已经按照here给出的例子,但我的表现很糟糕......

使用bokeh serve --show test.py启动脚本时,我看到带有五个圆圈的图表,但是当点击任何一个圆圈时,我既没有在命令控制台上看到任何打印输出,也没有看到第六个圆圈。

预期行为:我看到五个圆圈,当我点击其中一个圆圈时,我希望有一个打印输出("测试")并添加第六个圆圈(在坐标60/60处)。

也许这是错误的工具?无论如何,这是完整的代码(test.py):

from random import random

from bokeh.layouts import column
from bokeh.models import Button, TapTool
from bokeh.palettes import RdYlBu3
from bokeh.plotting import figure, curdoc

# create a plot and style its properties
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = 'black'
p.background_fill_color = 'black'
p.outline_line_color = None
p.grid.grid_line_color = None
p.circle([10, 20, 30, 40, 50], [60, 70, 20, 40, 50], size=20, color="navy", alpha=0.5)

def foo():
    print("test")
    p.circle([60], [60], size=20, color="navy", alpha=0.5)


taptool = p.select(type=TapTool)
taptool.callback = foo

# # put the button and plot in a layout and add to the document
curdoc().add_root(column(p))

P.S。:我希望能够点击圆圈,获取坐标,并在两个圆圈之间动态画线,我一个接一个地点击。

1 个答案:

答案 0 :(得分:0)

如果要添加/删除/修改绘图数据,最好将数据放入ColumnDataSource对象中,该对象可以进行回调。

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

p = figure(x_range=[0,100],y_range=[0,100],tools='tap')

source = ColumnDataSource(data={'x':[10, 20, 30, 40, 50],'y':[60, 70, 20, 40, 50]})

p.circle(x='x',y='y',size=20,source=source)

def foo(attr,old,new):
    new_source_data = {'x':source.data['x']+[60],'y':source.data['y']+[60]}
    source.data.update(new_source_data)

source.on_change('selected',foo)

curdoc().add_root(column(p))
相关问题