Tooltip position dosen't follow the current mouse cursor position

I use hovertool and set point_policy=“follow_mouse”,but It seems dosen’t work.The tooltip postion is still beside the line not the mouse cursor position.

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

x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

p = figure(title="Simple line example", x_axis_label="x", y_axis_label="y")

p.line(x, y, legend_label="Temp.", line_width=2)

tool_1 = HoverTool(
    tooltips=[
        ("x_value", "@x"),
        ("y_value", "@y"),
    ],
    mode='vline',
    point_policy="follow_mouse",
)
p.add_tools(tool_1)
show(p)

here is the figure


below is what I want

Thanks a lot!!

point_policy only applies to point-like glyphs, e.g. scatter markers. You want to set line_policy="none" (by default line tries to snap to the nearest point). FWIW I would not enable this combination without adding a vertical crosshair as is it otherwise very hard for a user to visually correlate the far-away mouse position with the line at all.

2 Likes

I think i got your point! Thanks!
May I ask another question about tooltips position?
Is it possible to fix tooltips position (like top left) when the mouse cursor moving around different postion?

Not presently. The original tooltip class was added very long ago as a private implementation detail and has a lot of baggage as a result. How best to surface a user-accessible and configurable version has been a source of some debate. In any case, it is still an open issue. Your best best for now would be to update the contents of a Div next to the plot, or update the values of text glyphs [1] inside the plot in a hover tool callback.


  1. Or possibly Label or LabelSet annotations. ↩︎

Thanks for your reply!
I’ll try!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.