首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在bokeh中获得x -coordinates和y坐标

如何在bokeh中获得x -coordinates和y坐标
EN

Stack Overflow用户
提问于 2019-05-27 18:54:45
回答 1查看 1.2K关注 0票数 0

我正面临着如何使用Tap工具在bokeh中显示图形上的x和y坐标的问题

我已经尝试了下面的代码。现在我想为攻丝工具写一个回调,我该怎么做呢?如何在单击正方形时显示x和y坐标

代码语言:javascript
复制
  from bokeh.models import ColumnDataSource, OpenURL, TapTool
  from bokeh.plotting import figure, output_file, show

  output_file("openurl.html")

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

  source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[2, 5, 8, 2, 
                         7]))


  p.square('x', 'y', color='green', size=20, source=source)

  taptool = p.select(type=TapTool)

  show(p)
EN

回答 1

Stack Overflow用户

发布于 2019-05-27 19:43:05

您可以通过编写一个在选择更改时调用的回调来获得点击时的x和y坐标。可以在source.selected.indices中找到当前所选字形的索引。这些索引可用于从source.data获取x和y坐标。这段代码在浏览器(F12)的控制台中打印x和y坐标。

代码语言:javascript
复制
from bokeh.models import ColumnDataSource, TapTool, CustomJS
from bokeh.plotting import figure, output_file, show

output_file("openurl.html")

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

source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5], y=[2, 5, 8, 2, 7]))

p.square('x', 'y', color='green', size=20, source=source)

callback = CustomJS(args=dict(source=source), code="""
    var selectedIndex = source.selected.indices;
    for (var i = 0; i < selectedIndex.length; i++) {
        console.log("Index:", selectedIndex[i]);
        console.log("x:", source.data['x'][selectedIndex[i]]);
        console.log("y:", source.data['y'][selectedIndex[i]]);
    }
""")

taptool = p.select(type=TapTool)
source.selected.js_on_change('indices', callback)
show(p)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56324711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档