简体   繁体   中英

Python Bokeh tool - How to display hovertool with datetime formatter xaxis from bokeh?

I using the bokeh vbar chart tool for my energy datas. If I use the tuple(string,string,..) for xaxis then it successfully worked. But I use the datetimetickformatter for xaxis then hover tool never display.

My sample code is here:

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, DatetimeTickFormatter,HoverTool
from bokeh.plotting import figure
from datetime import datetime
output_file("bar_colormapped.html")
dt1=datetime(2018,8,1)
dt2=datetime(2018,8,2)
dt3=datetime(2018,8,3)
dt4=datetime(2018,8,4)
dt5=datetime(2018,8,5)
dt6=datetime(2018,8,6)
fruits = [dt1,dt2,dt4,dt5,dt6]
counts = [5, 3, 4, 4, 6]

source = ColumnDataSource(data=dict(fruits=fruits, counts=counts))
tooltips=[
    ("val", "@counts")
]
p = figure(plot_height=350, toolbar_location=None, title="Fruit Counts",x_axis_type='datetime',tooltips=tooltips)
p.vbar(x='fruits', top='counts', width=0.9, source=source)

p.xaxis.formatter=DatetimeTickFormatter(
    minutes=["%M"],
    hours=["%H:%M"],
    days=["%d/%m/%Y"],
    months=["%m/%Y"],
    years=["%Y"],
)
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.y_range.end = 9
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
show(p)

代码结果在这里

This is in the documentation:

https://docs.bokeh.org/en/latest/docs/user_guide/tools.html#formatting-tooltip-fields

Presumably in your specific case, something like:

hover = HoverTool(tooltips=[('date', '@fruits{%F}'), ('val', '@counts')],
                  formatters=dict(fruits='datetime'))
p.add_tools(hover)

Also your bars are far too thin for hit testing. The units on a datetime scale is milliseconds since epoch, but your range covers many months. To the bars need to be much wider to show up on that scale. Eg width=10000000 yields:

在此处输入图片说明

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