Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show integer, not float, with hover tooltip in bokeh

Tags:

python

bokeh

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))
like image 238
captain ahab Avatar asked Mar 11 '15 23:03

captain ahab


2 Answers

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.

like image 110
WillZ Avatar answered Oct 18 '22 13:10

WillZ


Aha! Using @ instead of $ works.

hover.tooltips = [
    ("Days", "@x"),
    ("Return", "@y"),
]
like image 21
captain ahab Avatar answered Oct 18 '22 14:10

captain ahab