[英]How can I set custom datetime pattern on Bokeh hover formatter?
I'm trying to plot some values respect to the time using a line plot with Bokeh.我正在尝试使用散景线图绘制一些与时间有关的值。 My issue came when I tried to add a hover that shows the concrete value at some point of the plot.
当我尝试添加一个悬停来显示图的某个点的具体值时,我的问题就出现了。
I want to show a value, time
data (it works fine), but I think a yyyy-mm-dd
is imprecise for my purpose, so I want to redefine that pattern to add hours and minutes.我想显示一个
value, time
数据(它工作正常),但我认为yyyy-mm-dd
不符合我的目的,所以我想重新定义该模式以添加小时和分钟。
My code is something like that (you can download as a notebook here ):我的代码是这样的(你可以在这里下载笔记本):
from datetime import datetime, timedelta
from bokeh.plotting import figure, show
from bokeh.models import HoverTool
import pandas as pd
import numpy as np
today = datetime.today()
date_range = pd.date_range(today, today + timedelta(days=1),
freq=timedelta(minutes=15))
values = np.random.randint(-10, 10, size=len(date_range)).cumsum()
data = pd.DataFrame({'date': date_range, 'value': values})
hover = HoverTool(tooltips=[('value', '@value'), ('date', '@date{%F}')],
formatters={'date': 'datetime'})
plt = figure(x_axis_type='datetime', tools=[hover])
plt.line(x='date', y='value', source=data)
show(plt)
The output is like that:输出是这样的:
So my question is as follows:所以我的问题如下:
Can anyone explain me how to modify the datetime format pattern on the hover?谁能解释我如何修改悬停时的日期时间格式模式?
You can add the time in hours and minutes by adding %H:%M
to @date
, like this: 您可以通过将
%H:%M
添加到@date
来以小时和分钟为单位添加时间,如下所示:
hover = HoverTool(tooltips=[('value', '@value'), ('date', '@date{%F %H:%M}')],
formatters={'date': 'datetime'})
The scales are described in the DatetimeTickFormatter documentation. 在DatetimeTickFormatter文档中描述了比例。
For supplyment: bokeh version 2.4.1供应: bokeh version 2.4.1
formatters
: formatters
:hover = HoverTool(tooltips=[('value', '@value'), ('date', '@date{%F}')],
formatters={'@date': 'datetime'})
------------line------------------ - - - - - - 线 - - - - - - - - -
hover = HoverTool(
tooltips="""
<div>
<h3>@date{%F}</h3>
<div>
<strong>value: </strong>
<span style="font-size: 30px; font-weight: bold; color: #696;">@value</span>
</div>
</div>
""",
formatters={'@date': 'datetime'},
mode='vline'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.