繁体   English   中英

如何制作交互式散景图?

[英]How to make an interactive bokeh plot?

我第一次尝试使用bokeh在网络浏览器上进行交互式绘图。 我绘制了一些圆,并且在单击其中一个圆时想要一些交互(回调)。 我已经按照此处给出的示例进行操作,但是我失败了……

当使用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))

PS:我希望能够单击这些圆,获取它们的坐标,并在我一个接一个单击的两个圆之间动态绘制线。

当您想添加/删除/修改打印数据时,最好将数据放在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))

暂无
暂无

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

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