簡體   English   中英

Python Bokeh工具-如何從bokeh顯示帶有日期時間格式化程序xaxis的hovertool?

[英]Python Bokeh tool - How to display hovertool with datetime formatter xaxis from 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)

代碼結果在這里

在文檔中:

https://docs.bokeh.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產生:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM