25

I have a simple graph of X-Y data points. I want my Bokeh figure to show me the integer value of each datapoint when I hover over it. I am close to getting what I want but when I hover over the data point, it shows a float and then higher up, it uses scientific notation. Is there a way to have the hover tool only return the integer values of X and Y and not use scientific notation?

Here is some example code:

from bokeh.plotting import *
from bokeh.models import HoverTool

x = range(1,101)
y = [i*i for i in x]

TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select, hover"

p = figure(x_axis_label = "Days",
       y_axis_label = "Return",
       tools=TOOLS)
p.circle(x, y)

#adjust what information you get when you hover over it
hover = p.select(dict(type=HoverTool))
hover.tooltips = [
    ("Days", "$x"),
    ("Return", "$y"),
]

show(VBox(p))
captain ahab
  • 1,029
  • 1
  • 11
  • 18

2 Answers2

44

Adding my two cents. I figured out by you can control the decimal points by using the following code:

hover.tooltips = [
    ("Days", "@x{int}"), # this will show integer, even if x is float
    ("Return", "@y{1.11}"), # this will format as 2-decimal float
]

Hope this helps.

WillZ
  • 3,775
  • 5
  • 30
  • 38
  • 2
    Will, curious if you know how to scale y and then format it. for example, let's say you want to multiply y by 100 and report as a percentage. any thoughts on how to specify that? – Chris Aug 29 '16 at 23:42
  • This is great. Do you know if there is notation for formatting dates like this? I didn't manage to find this in the documents; do you happen to have a link where this is located? Thanks! – ryanjdillon Oct 30 '16 at 11:21
  • @Chris sorry not aware of a flag for percentage formatting. I suppose you already tried Python's formatting flags? What I'd do is to create another column in your data source and plot that instead, not perfect but might get you there. – WillZ Nov 01 '16 at 11:20
  • 1
    @ryanjdillon I think I found these in their Google Group archives. Unfortunately it's not in the docs as far as I know. I haven't chased down in the source code yet to find out how it's parsed but it's a bit different to the traditional python format strings. However, things like '+1.11' still works, i.e. puts a sign in front of numbers. – WillZ Nov 01 '16 at 11:38
13

Aha! Using @ instead of $ works.

hover.tooltips = [
    ("Days", "@x"),
    ("Return", "@y"),
]
Kirell
  • 9,228
  • 4
  • 46
  • 61
captain ahab
  • 1,029
  • 1
  • 11
  • 18