gpt4 book ai didi

python - 如何在 Bokeh Hovertool 中仅显示整数

转载 作者:行者123 更新时间:2023-11-30 22:34:05 24 4
gpt4 key购买 nike

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

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

我添加了一个悬停工具来显示图表中绘图的 y 值,问题是悬停工具沿绘制的线显示浮点值。为了视觉效果,我已经绘制了直线和圆圈,但即使我删除了线条,这仍然是正确的 Hovertool text

将使用这些图表的人询问是否可以在数据更改的点仅显示整数我尝试将 y 值转换为整数,但这并不是驱动悬停工具获取它的原因值。

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

这是我的悬停工具的代码

 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)

关于python - 如何在 Bokeh Hovertool 中仅显示整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44958328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com