简体   繁体   English

如何制作交互式散景图?

[英]How to make an interactive bokeh plot?

I am trying bokeh for the first time to make an interactive plot on a web browser. 我第一次尝试使用bokeh在网络浏览器上进行交互式绘图。 I plot some circles, and want some interaction (callback) when I click on one of the circles. 我绘制了一些圆,并且在单击其中一个圆时想要一些交互(回调)。 I have followed the example given here , but I am failing badly... 我已经按照此处给出的示例进行操作,但是我失败了……

When starting the script with bokeh serve --show test.py I see the plot with the five circles, but when clicking on any circle,I neither see any print-output on the command console nor the sixth circle drawn. 当使用bokeh serve --show test.py启动脚本时,我看到有五个圆圈的情节,但是单击任何圆圈时,我既看不到命令​​控制台上的任何打印输出,也看不到绘制的第六个圆圈。

Expected behaviour: I see five circles, and when I click on one of them I expect to have a printout ("test") and added the sixth circle (at coordinates 60/60). 预期的行为:我看到五个圆圈,当我单击其中的一个时,我希望有一个打印输出(“测试”)并添加第六个圆圈(在坐标60/60处)。

Maybe its the wrong tool altogether? 也许完全是错误的工具? Anyway, here is the complete code ( test.py ): 无论如何,这是完整的代码( 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: I want to be able to click on the circles, get their coordinates, and draw lines dynamically between two circles I click on one after another. PS:我希望能够单击这些圆,获取它们的坐标,并在我一个接一个单击的两个圆之间动态绘制线。

When you want to add/remove/modify plotted data, it's good to put your data in a ColumnDataSource object, which can have a callback. 当您想添加/删除/修改打印数据时,最好将数据放在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