簡體   English   中英

散景 (Python):在懸停工具提示中格式化日期時間

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

我正在嘗試將我的工具提示格式化為 Month Year 格式,但它似乎不起作用?

這是我的代碼:

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'}

暫無
暫無

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

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