'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.enter image description here



Solution 1:[1]

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)

Hover with Date

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mosc9575