简体   繁体   中英

Bokeh HoverTool datetime formatter not working

My code for the bokeh HoverTool is the following:

p = figure(
        plot_height=250,
        x_axis_type='datetime',
    )
p.vbar(x=data_df['date'].dt.to_pydatetime(), top=data_df['data'].values, width=datetime.timedelta(1))
hover_tool = HoverTool(
        tooltips=[('Count', '@top'), ('Date', '@x')], mode='vline', formatters={'$x': 'datetime'}
    )
p.add_tools(hover_tool)

I still get the numeric format of the date as can be seen on the image. I tried formatters={'@x': 'datetime'} with no luck. 在此处输入图像描述

Your solution works, if you use $x instead of @x and add one of the listed formats supported by the DatetimeTickFormatter to ('Date', '$x') like ('Date', '$x{%F}') . There are plenty of options and you can select the one you prefere the most.

Minimal Example

import pandas as pd
from bokeh.plotting import show, figure, output_notebook
from bokeh.models import HoverTool, ColumnDataSource
output_notebook()

data_df = pd.DataFrame({'date':pd.date_range('2022-05-13', freq='D', periods=10), 'top':list(range(10))})

p = figure(
        plot_height=250,
        x_axis_type='datetime',
    )
p.vbar(x=data_df['date'], top=data_df['top'], width=pd.Timedelta('12H'))
hover_tool = HoverTool(
        tooltips=[('Count', '@top'), ('Date', '$x{%F}')], mode='vline', formatters={"$x": "datetime"}
    )
p.add_tools(hover_tool)
show(p)

悬停日期

Comment:

I don't know why there is no working default, but maybe because there are so many options, that any default would be somehow wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM