简体   繁体   English

散景 (Python):在悬停工具提示中格式化日期时间

[英]Bokeh (Python): Format DateTime in Hover tooltip

I'm trying to format my tool tip to Month Year format but it doesn't seems to work?我正在尝试将我的工具提示格式化为 Month Year 格式,但它似乎不起作用?

Here's my code:这是我的代码:

import numpy as np # linear algebra
import pandas as pd 
from bokeh.plotting import figure, show, output_file, output_notebook
from bokeh.palettes import Spectral11, colorblind, Inferno, BuGn, brewer
from bokeh.models import HoverTool, value, LabelSet, Legend, ColumnDataSource,LinearColorMapper,BasicTicker, PrintfTickFormatter, ColorBar
import datetime
from bokeh.models import DatetimeTickFormatter

TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom,tap"
p = figure(title="Total Workplace Incidents", x_axis_type="datetime", y_axis_type="linear", plot_height = 400,
           tools = TOOLS, plot_width = 800)

p.xaxis.axis_label = 'Month/Year'
p.yaxis.axis_label = 'Number of Incidents'

p.line(g['Date'], g['ISMS\nReport#'],line_color="blue", line_width = 3)


p.xaxis.formatter=DatetimeTickFormatter(
        days= ["%b %y"],
        months=["%b %y"],
        years=["%b %y"]
    )


p.select_one(HoverTool).tooltips = [
    ('Month/Year', '@x {%b %y}'),
    ('Number of Incidents', '@y'),
]


output_file("test.html", title="Line Chart")
show(p)

Will appreciate some advise please?请欣赏一些建议? Thank you.谢谢你。

Looks like you just need two things:看起来你只需要两件事:

  • remove the space between the fieldname @x and the format {%b, %y}删除字段名@x和格式{%b, %y}之间的空格
  • add a formatter indicating that x is a datetime添加一个格式化程序,指示x是日期时间
p.select_one(HoverTool).tooltips = [
    ('Month/Year', '@x{%b %y}'),
    ('Number of Incidents', '@y'),
]

p.select_one(HoverTool).formatters = {'x':'datetime'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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