简体   繁体   English

在散景中修改悬停时的显示格式

[英]Modify the format of display on hover in Bokeh

I have datetime on x-axis and I was trying to plot it as a datetime, but apparently, according to this Bokeh can only have number axes. 我在x-axis有日期时间,我试图将其绘制为日期时间,但是显然,根据 Bokeh只能有数字轴。 Unless that's changed by now, then please let me know. 除非现在没有更改,否则请告诉我。 But I was wondering if there's maybe at least a way to display datetime on hover rather than timestamp (something like 153286000 )? 但是我想知道是否至少有一种方法可以在悬停而不是时间戳上显示日期时间(例如153286000 )?

p.select_one(HoverTool).tooltips = [('Datetime', '@x'),('Position', '@y')]

I tried adding .to_datetime() but that didn't work. 我尝试添加.to_datetime()但是没有用。

You need to set the x_axis_type = "datetime" and use formatters for the datetime like this: 您需要设置x_axis_type = "datetime"并为日期时间使用格式化程序,如下所示:

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

See full example below (Bokeh v1.1.0). 请参阅下面的完整示例(Bokeh v1.1.0)。 See also Bokeh documentation on tooltips formatting. 另请参阅Bokeh文档中有关工具提示格式的信息。

import numpy as np
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure
from bokeh.sampledata.stocks import AAPL

def datetime(x):
    return np.array(x, dtype = np.datetime64)

source = ColumnDataSource(data = {'date'      : datetime(AAPL['date'][::10]),
                                  'adj close' : AAPL['adj_close'][::10],
                                  'volume'    : AAPL['volume'][::10]})

p = figure(plot_height = 250, x_axis_type = "datetime", sizing_mode = "scale_width")

p.background_fill_color = "#f5f5f5"
p.grid.grid_line_color = "white"
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.axis.axis_line_color = None

p.line(x = 'date', y = 'adj close', line_width = 2, color = '#ebbd5b', source = source)

hover = HoverTool(mode = 'vline')
hover.tooltips = [('date', '@date{%F}'), ('close', '$@{adj close}{%0.2f}'), ('volume', '@volume{0.00 a}')]
hover.formatters = {'date': 'datetime', 'adj close' : 'printf'}
p.add_tools(hover)

show(p)

Result: 结果:

在此处输入图片说明

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

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