如何在 Bokeh Hovertool 中只显示整数

How to show only integers in Bokeh Hovertool

我构建了一个 Flask 应用程序,它在 Bokeh 中绘制了多个图表。

x 值是表示天数或周数的日期时间对象,y 值是被跟踪的值。每一天都有一个y值,是一个整数。

我添加了一个 hovertool 来显示图中绘图的 y 值,问题是 hovertool 沿绘图线显示了一个浮点值。为了视觉效果,我画了线条和圆圈,但即使我删除了线条,这仍然是正确的

将使用这些图表的人询问是否可以在数据发生变化的点上仅显示整数 我试图将 y 值转换为整数,但这并不是驱动 hovertool 的原因获取它的值。

如果可能的话,我想更改的另一件事是增加 hovertool 中文本的字体大小。我查看了 http://docs.bokeh.org/en/latest/docs/user_guide/tools.html#hovertool and http://docs.bokeh.org/en/latest/docs/reference/models/tools.html 上的文档,但我无法设置它。

这是我的 hovertool 的代码

 hover = HoverTool(tooltips=[
(title, "($y)")

稍后....

   fig = figure(plot_width=500,plot_height=500,title=title,x_axis_type='datetime',tools=[hover])    

如果要更改悬停工具中值的格式,可以在指定每个悬停工具字段时指定格式。参见:http://docs.bokeh.org/en/latest/docs/user_guide/tools.html#formatting-tooltip-fields.

这是一个不同格式的例子:

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

output_file("toolbar.html")

source = ColumnDataSource(data=dict(
    x=[1.8, 2, 3, 4, 5],
    y=[2.2, 5, 8.234, 2, 7.22],
    n_id=[1.8,2.8,2.2,2.4,2.5]))

hover = HoverTool(
    tooltips=[
        ( 'x','$x{0}'),
        ( 'y','@y{0.0}'),
        ('id','@n_id{0}')
    ],
)

p = figure(plot_width=400, plot_height=400, tools=[hover],
           title="Mouse over the dots")

p.circle('x', 'y', size=20, source=source)

show(p)

如果你想更好地控制外观,你需要编写自定义工具提示:http://docs.bokeh.org/en/latest/docs/user_guide/tools.html#custom-tooltip

您可以使用它并像这样组合格式化程序:

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

output_file("toolbar.html")

source = ColumnDataSource(data=dict(
    x=[1.8, 2, 3, 4, 5],
    y=[2.2, 5, 8.234, 2, 7.22],
    n_id=[1.8,2.8,2.2,2.4,2.5]))

hover = HoverTool(
    tooltips="""
    <div>
        <div>
            <span style="font-size: 17px; font-weight: bold;">x: @x{0.0}</span>
        </div>
        <div>
            <span style="font-size: 15px; color: #966;">y: @y{0.00}</span>
        </div>
        <div>
            <span style="font-size: 23px; color: #966;">id: @n_id{0}</span>
        </div>
    </div>
    """
)

p = figure(plot_width=400, plot_height=400, tools=[hover],
           title="Mouse over the dots")

p.circle('x', 'y', size=20, source=source)

show(p)