Python Bokeh - Assign taptool to a subset of Glyphs

只谈情不闲聊 提交于 2019-12-12 03:45:41

问题


Hi have a timeserie to which I add circle glyphs, representing 2 different type of data.

I already managed to assign a different tooltip to each of them by using the suggestion presented here: https://stackoverflow.com/a/37558475/4961888

I was hoping I could use a similar syntax to assign a specific subset of the glyph to a taptool, that would bring me to a url present in the source of those glyphs.

so I tried:

    fig = figure(x_axis_type="datetime", height=200, tools="tap")
    fig.line(x_range, y_range)  
    news_points = fig.circle(x='timestamp', y='y', fill_color="green", size=5, source=news_source)

    url = "@url"
    taptool = fig.select(type=TapTool)
    taptool.renderers.append(news_points)
    taptool.callback = OpenURL(url=url)

But i receive:

AttributeError: '_list_attr_splat' object has no attribute 'renderers'

It seems to me that the taptool has a different way of being assigned glyphs than the hovertool, and I couldn't find any documentation on the web regarding my problem. I would be glad if someone with a better grip of the package could help me wrap my head around it.


回答1:


fig.select returns a list, so you'll want to access the first (and only, I'd assume) element.

fig = figure(x_axis_type="datetime", height=200, tools="tap")
fig.line(x_range, y_range)  
news_points = fig.circle(x='timestamp', y='y', fill_color="green", size=5, source=news_source)

url = "@url"
taptool = fig.select(type=TapTool)[0]
taptool.renderers.append(news_points)
taptool.callback = OpenURL(url=url)

OR

fig = figure(x_axis_type="datetime", height=200, tools="tap")
fig.line(x_range, y_range)  
news_points = fig.circle(x='timestamp', y='y', fill_color="green", size=5, source=news_source)

url = "@url"
taptool = fig.select_one(TapTool)
taptool.renderers.append(news_points)
taptool.callback = OpenURL(url=url)



回答2:


If you are using bokeh 0.13.0 you achieve this by assigning the renderers as a list. Instead of taptool.renderers.append(glyph) You can do taptool.renderers= [glyph,]

Something along the lines

fig = figure(x_axis_type="datetime", height=200, tools="tap")
fig.line(x_range, y_range)  
news_points = fig.circle(x='timestamp', y='y', fill_color="green", size=5, 
source=news_source)

url = "@url"
taptool = fig.select(type=TapTool)[0]
taptool.renderers= [news_points,]
taptool.callback = OpenURL(url=url)```

Otherwise you might get an extra error str has no attribute append From the documentation renderers can also accept a list, This is especially important where you have multiple renderers on the graph otherwise it takes all the renderers on the plot.



来源:https://stackoverflow.com/questions/43283717/python-bokeh-assign-taptool-to-a-subset-of-glyphs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!