简体   繁体   中英

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?

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}
  • add a formatter indicating that x is a datetime
p.select_one(HoverTool).tooltips = [
    ('Month/Year', '@x{%b %y}'),
    ('Number of Incidents', '@y'),
]

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

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