Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HoverTool showing canvas coordinates not data coordinates #4861

Closed
jcorb opened this issue Jul 22, 2016 · 11 comments · Fixed by #5406
Closed

HoverTool showing canvas coordinates not data coordinates #4861

jcorb opened this issue Jul 22, 2016 · 11 comments · Fixed by #5406

Comments

@jcorb
Copy link

jcorb commented Jul 22, 2016

I don't think this is mentioned in the other hover tool issues, but I might have missed it.

Using $x, $y in HoverTool displays the canvas coordinates, not the data coordinates as I believe it is supposed to. Using $sx, $sy displays the canvas coordinates as well.

e.g.

from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool

output_file("hover.html")

hover = HoverTool(
            tooltips=[
                ("index", "$index"),
                ("data (x,y)", "($x, $y)"),
                ("canvas (x,y)", "($sx, $sy)")
                ])

TOOLS = [hover]

p = figure(plot_width=400, plot_height=400, title=None, tools=TOOLS)

p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)

show(p)

produces

screen shot 2016-07-22 at 7 46 20 am

@birdsarah
Copy link
Member

Thanks for the report. I actually thought $x was meant to give screen space, but I just looked at the docs (http://bokeh.pydata.org/en/latest/docs/reference/models/tools.html#bokeh.models.tools.HoverTool.tooltips) and you're totally right.

@damianavila damianavila added this to the short-term milestone Jul 25, 2016
@bgbg
Copy link
Contributor

bgbg commented Jul 27, 2016

I can confirm this behavior on Mac -- in Chrome and Safari.

@bgbg
Copy link
Contributor

bgbg commented Jul 27, 2016

A quick workaround: when referencing the column names using the "@" notation, the tooltips are presented correctly

from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool

hover = HoverTool(
            tooltips=[
                ("index", "$index"),
                ("data (using $) (x,y)", "($x, $y)"),
                ("data (using @) (x,y)", "(@x, @y)"),
                ("canvas (x,y)", "($sx, $sy)")
                ])

TOOLS = [hover]
p = figure(plot_width=400, plot_height=400, title=None, tools=TOOLS)
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)

image

@Prikers
Copy link

Prikers commented Oct 6, 2016

I had noted this issue as well. Even in the documentation, the example shows the canvas coordinates, not the data coordinates.
@bgbg thank you for the workaround. If it can be useful for anyone: when using bar chart @y references to the middle value of the bar, not the height of it. In that case it seems that @height works fine.
I did not find the list of all supported keywords with the @ notation. Did I miss it?

@bryevdv
Copy link
Member

bryevdv commented Oct 6, 2016

@Prikers the @ notation can be used to reference any column in the associated column data source. There is no set list, you can use any column name you like.

This definitely seems like a bug introduced at some point. The hover tool logic is quite convoluted to handle all the different cases, I've been meaning to try and factor and simplify it but have not had the opportunity to yet. If anyone is interested in poking around, the relevant files are here:

https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/models/tools/inspectors/hover_tool.coffee
https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/core/util/templating.coffee

in particular it would seem data_x and data_y are what are getting set incorrectly as a first guess.

@bryevdv
Copy link
Member

bryevdv commented Oct 6, 2016

I actually find this line suspicious:

https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/models/tools/inspectors/hover_tool.coffee#L281

and wonder if it should just be removed.

@bryevdv
Copy link
Member

bryevdv commented Oct 6, 2016

ping @mattpap too

@mattpap
Copy link
Contributor

mattpap commented Oct 25, 2016

The hover tool logic is quite convoluted to handle all the different cases (...)

The logic may in fact be convoluted, but the syntax is pretty straightforward. $ is used to reference special variables, i.e. something you won't find in a data source and @ to reference column names. So, $x and @x may or may not be the same thing. For example, if there is a circle at (0, 0) with data radius of 1 and hover tool is set to follow mouse, then @x will be always 0 when hovering this point, but $x will vary in [-1, 1] interval. Otherwise, if mouse following is off, then $x == @x. EDIT: well, it's more complicated than that, because there are anchor points to consider, etc.

@mattpap
Copy link
Contributor

mattpap commented Oct 25, 2016

Anyway, the bug was trivial and boiled down to shadowing of variables. Fixed in PR #5406.

@adamsiembida
Copy link

I'm running Bokeh 0.12.5, and I still seem have this issue on line plots. Here is an example:

import numpy as np
import bokeh.plotting as plt
import bokeh.models as pltm

x = np.arange(100)
y = x**4

fig = plt.figure()
fig.add_tools(pltm.HoverTool())
fig.line(x, y)
plt.show(fig)

Here is the result. You can see that I get two different values of y, depending on where my cursor is positioned. The way I understand it, canvas should give the position of the cursor, and the other value should display the value of the indicated data point.

1

2

@bryevdv
Copy link
Member

bryevdv commented Jun 9, 2017

@w1res there might be something specific to lines which are more complicated due to their connectivity. However, please open a new issue so this can be tracked and prioritized independently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants