python - Bokeh (Python): Format DateTime in Hover tooltip

标签 python tooltip bokeh

我正在尝试将工具提示格式化为月年格式,但似乎不起作用?

这是我的代码:

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)

请您提供一些建议吗? 谢谢。

最佳答案

看起来你只需要两件事:

  • 删除字段名称 @x 和格式 {%b, %y} 之间的空格
  • 添加一个格式化程序,指示 x 是日期时间
p.select_one(HoverTool).tooltips = [
    ('Month/Year', '@x{%b %y}'),
    ('Number of Incidents', '@y'),
]

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

关于python - Bokeh (Python): Format DateTime in Hover tooltip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207015/

相关文章:

python - 被 tensorflow 输出混淆

c#:在子项上显示工具提示

python - Bokeh - 将颜色贴图应用于线条集

javascript - 如何实现 JavaScript 回调来更改 Bokeh 图标题

c# - 如何使工具提示显示格式化文本?

python - Bokeh 图上的多个 slider

python - 将大文件从 URL 直接流式传输到 gzip 文件

python - Pandas 和数据框 : How to transform a ordinal variable in a binary variable?

python - 如何在 PyCrypto AES CTR 模式下设置 block 大小

javascript - 如果为空值,则引发输入工具提示