如何在散景中使用悬停工具提示显示整数,而不是浮点数

时间:2022-08-26 23:35:48

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?

我有一个简单的X-Y数据点图。当我将鼠标悬停在它上面时,我希望我的Bokeh图显示每个数据点的整数值。我接近得到我想要的东西但是当我将鼠标悬停在数据点上时,它显示一个浮点数然后更高,它使用科学记数法。有没有办法让悬停工具只返回X和Y的整数值而不使用科学记数法?

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))

2 个解决方案

#1


28  

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.

希望这可以帮助。

#2


9  

Aha! Using @ instead of $ works.

啊哈!用@代替$作品。

hover.tooltips = [
    ("Days", "@x"),
    ("Return", "@y"),
]

#1


28  

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.

希望这可以帮助。

#2


9  

Aha! Using @ instead of $ works.

啊哈!用@代替$作品。

hover.tooltips = [
    ("Days", "@x"),
    ("Return", "@y"),
]