每当我绘制任何具有“hh:mm:ss”格式时间的图表并使用悬停工具在图中任意点查看相同格式的时间戳时,我都会看到一个具有指数度数的十进制数。
例如,时间是“23:04:12”,我会看到一些值为“5.8373e7”的随机数。
请使用示例“Dummy.csv”运行“Test.ipynb”代码,这两个代码都在以下谷歌驱动器链接中给出,以便更好地阐明我的问题。
https://drive.google.com/drive/folders/1_FAp4ASuwBJxl6Mtp0xQwxSQ1Zcl9vvx
如果有人能为我提供此问题的解决方案,以便我可以在悬停工具上看到“hh:mm:ss”格式的时间戳,我将非常感激。
提前致谢!
可以为您的 Hovertool 启用预定义的格式化方案。
您可以使用常见的格式将列名称和类型
"numeral"
、"datetime"
和 "printf"
作为字典添加到参数 formatters
。
tooltips
采用(名称,字段)对的列表,描述点击时悬停工具应显示的内容。在这里您可以使用格式化快捷方式。
对于“日期时间”格式,您可以查看 DatetimeTickFormatter 以获取所有支持的快捷方式的列表。
还将要激活 HoverTool 的渲染器添加到
renderers
参数,并将该工具添加到绘图中。
另请参阅下面的最小示例。
import pandas as pd
from bokeh.models import HoverTool
from bokeh.plotting import figure, show, output_notebook
output_notebook()
data = {
'datetime': ['2024-04-14', '2024-04-15', '2024-04-16', '2024-04-17', '2024-04-18', '2024-04-19', '2024-04-20'],
'opening_price': [1, 1, None, 0, None, -1, -1],
}
df = pd.DataFrame(data)
df['datetime'] = pd.to_datetime(df['datetime'])
df = df.set_index('datetime')
p = figure(x_axis_type='datetime', width=300, height=300)
circles = p.scatter(x='datetime', y='opening_price', source=df)
hovertool = HoverTool(
tooltips = [('datetime', "@datetime{%Y-%m-%d %H:%M:%S}")],
formatters = {"@datetime": "datetime"},
renderers = [circles]
)
p.add_tools(hovertool)
show(p)