How to add hover tooltip for a plot which contains multiple data sources?

Hi,
I am new to Bokeh. The doc shows I should do something like this to create a tooltip.

source = ColumnDataSource(data=dict(
 x=[1, 2, 3, 4, 5],
 y=[2, 5, 8, 2, 7],
 desc=['A', 'b', 'C', 'd', 'E'],
))

hover = HoverTool(tooltips=[
 ("index", "$index"),
 ("(x,y)", "(@x, @y)"),
 ("desc", "@desc"),
])

so if the plot is using multiple data sources, it seems I am not able to use @xxx to specify a column.

and if i am not using a ColumnDataSource, how to make the mapping work? e.g. to add tooltip for https://bokeh.pydata.org/en/latest/docs/gallery/candlestick.html

Hi,

If you are using cover tools, my recommendation is to always make your own ColumnDataSources, so that you know and control exactly what the column names are. Bokeh will *always* use a CDS, but if you don't provide one yourself (like the example below) then Bokeh makes one, with some default column names depending on the glyph fields (e.g. "x", "radius", etc).

If the data are all the same length, it's possible to drive multiple glyphs for a single CDS, in which case you can just make a single hover tool with whatever columns you want, from that one CDS. That's another good reason to create your own CDS explicitly.

If the glyphs have different data lengths then you will have to make separate CDS for them. All columns in a given CDS must alway be the *same length* at all times (think of them like cheap Pandas DataFrames). In this case you will need to make multiple hover tools. By default hover tools try to hover for every glyph, but you can restrict different hover tools to different glyphs by using the .renderers property on the tool.

Thanks,

Bryan

···

On Dec 5, 2017, at 03:02, [email protected] wrote:

Hi,
I am new to Bokeh. The doc shows I should do something like this to create a tooltip.

source = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
desc=['A', 'b', 'C', 'd', 'E'],
))

hover = HoverTool(tooltips=[
("index", "$index"),
("(x,y)", "(@x, @y)"),
("desc", "@desc"),
])

so if the plot is using multiple data sources, it seems I am not able to use @xxx to specify a column.

and if i am not using a ColumnDataSource, how to make the mapping work? e.g. to add tooltip for https://bokeh.pydata.org/en/latest/docs/gallery/candlestick.html

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/3ba5d60f-cfa1-4b16-9049-271a2049adde%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

If I have multiple HoverTools on the same figure, how do I add multiple CDS?

I already have a HoverTool setup for the line plot in the above image, but I’d also like to have one for the vertical Span which uses a different data set.

Span is an annotation. Annotations cannot have hovers right now.

Ah. I suspected as much. Thank you for confirming.

What if I overlaid some lines where the spans are. How could I attach a separate CDS to them (separate from the trend line)?

For the little more context, the horizontal trend line has my test data, and the vertical lines has annotations. I already have tooltips on on the test data points giving more information about each point, but I’d like the vertical lines to show their annootations when hovered over.

@tmzhuang

See the following link for one way to add a hover tooltip that is associated with your span. Its conceptually similar to your thought about adding a line. Either should work depending on the mode you choose for activating. I think regardless you’d want to set the alphas (transparency properties) for that added glyph to 0.0 so it is not visible from the user’s perspective.

https://discourse.bokeh.org/t/spans-and-hovertool/5171/6

Regarding the second part of the question, have you tried generating two separate hover tools and associating the first one with the trendline and the second with the vertical line/span. The HoverTool has a renderers property where you can list specific glyphs that you want to be associated with it rather than all glyphs on the plot.

@_jm
I’ll give the work around you mentioned a try – it sounds promising.

With regards to tying data for the annotations, here is some sample code:

I’m not too sure how to tie the data for the annotations since it’s not part of the CDS I pass to figure (since the annotation data is separate).

@tmzhuang

The column data sources are not properties of the figure but rather the glyphs rendered in the figure.

I looked at the code snippet you provided, and it looks very well-written but in the interest of time, I chose to assemble a very small example of how to accomplish your goal so you can extend and integrate as it best fits into your larger application.

Here’s an illustrative example of adding hover tools that are effectively associated with the spans, and separate column data sources attached to populate with information of interest.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
"""
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, Span, HoverTool


p = figure(width=400, height=300, x_range=(0.0,1.0), y_range=(0.0,1.0))
p.line(x=[0.0,1.0], y=[0.5,0.5])

# Spans
data = dict(x=[1./3.,2./3.], ev=['Something interesting happened here...','Here too...'])
source = ColumnDataSource(data=data)

S = [Span(location=x, dimension='height', line_color='#ff0000', line_alpha=0.2) for x in data['x']]
_ = [p.add_layout(s) for s in S]

# Hover tool support for spans
r = p.circle(x='x', y=0.5, source=source, size=30, alpha=0.0)

h = HoverTool(renderers=[r], tooltips=({'x': '@x', 'Event': '@ev'}), mode='vline')
p.add_tools(h)

show(p)

@_jm
This was exactly what I was looking for. Thanks so much for pointing me in the right direction (and the detailed example).

I had to collect my previous renderers for the line plots so that the previous hovertool I created wasn’t general to the figure, but specific to the line/scatter renderers. (In case anyone else runs into my situation.)