Python Bokeh工具-如何从bokeh显示带有日期时间格式化程序xaxis的hovertool?

时间:2018-08-09 11:22:52

标签: python charts bokeh

我使用bokeh vbar图表工具获取能源数据。如果我将元组(string,string,..)用于xaxis,则它可以成功工作。但是我将datetimetickformatter用于xaxis,所以悬停工具从不显示。

我的示例代码在这里:

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)

Code result here

1 个答案:

答案 0 :(得分:1)

这在文档中:

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

大概在您的特定情况下,类似于:

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

此外,您的条形太薄,无法进行命中测试。日期时间刻度上的单位是自纪元以来的毫秒数,但您的范围涵盖了数月。条形图需要更宽才能显示在该比例尺上。例如。 width=10000000产生:

enter image description here