Bokeh TapTool,如何使用多个TapTool

2024-06-01 03:24:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据源,希望使用TapTool分别选择它们。当我点击圆圈时,它将我重定向到一个网页。这适用于一个情节,但我不能让它适用于两个不同的情节。 我尝试过添加工具功能,但根本不起作用

source1 = ColumnDataSource(data=dict(
    x=df1["xPos"].values,
    y=df1["yPos"].values,
    url=df1["Url"].values
source2 = ColumnDataSource(data=dict(
    x=df2["xPos"].values,
    y=df2["yPos"].values,
    url=df2["Url"].values

plot = figure(width= 500, height= 500, tools="pan,wheel_zoom,save,reset,tap", 
              x_range=Range1d(-10.1, 10.1), y_range=Range1d(-10.1, 10.1), title='TapToolTest')

plot1 = plot.circle('x', 'y', size=20, source=source1)
plot2 = plot.circle('x', 'y', size=10, source=source2)

url = "@url"
#taptool = plot.select_one(TapTool)
#taptool.renderers = [plot1]
#taptool.callback = OpenURL(url=url) #This methode works, but only for plot 1

plot.add_tools(TapTool(renderers=[plot1], callback = OpenURL(url=url)))
plot.add_tools(TapTool(renderers=[plot2], callback = OpenURL(url=url)))

show(plot)

Tags: urlplotcallbacktoolsrenderersdf1valuesdf2
1条回答
网友
1楼 · 发布于 2024-06-01 03:24:37

在您的示例中,有1个绘图和2个渲染器。以下代码适用于所有圆圈(从Bokeh v2.1.1开始):

from bokeh.models import ColumnDataSource, OpenURL, TapTool
from bokeh.plotting import show, figure

p = figure(plot_width=400, plot_height=400,
           tools="tap", title="Click the Dots")

source1 = ColumnDataSource(data=dict(
    x=[1, 2, 3, ],
    y=[2, 5, 8, ],
    color=["navy", "orange", "olive", ]
    ))
source2 = ColumnDataSource(data=dict(
    x=[4, 5, 6],
    y=[2, 7, 4],
    color=["firebrick", "gold", "skyblue"]
    ))

p.circle('x', 'y', color='color', size=20, source=source1)
p.circle('x', 'y', color='color', size=30, source=source2)

url = "http://www.colors.commutercreative.com/@color/"
taptool = p.select(type=TapTool)
taptool.callback = OpenURL(url=url)

show(p)

相关问题 更多 >