在点的确切位置显示Bokeh日期时间轴的标签

时间:2019-01-25 16:12:41

标签: python bokeh

我正在使用Bokeh的日期时间轴。在Bokeh数据源中,我以numpy datetime格式设置了x,其他的则是y数字。我正在寻找一种在该点正下方显示x datetimx轴的标签的方法。我希望Bokeh通过数据源显示我提供的确切日期时间,而不是近似值!例如,我提供5:15:00并在相关点之前的某个地方显示5:00:00,我计划每1小时将数据流式传输到图表中,我希望每次显示5点。因此,我需要5个日期时间标签。我怎样才能做到这一点?我尝试了p.yaxis[0].ticker.desired_num_ticks = 5,但没有帮助。散景仍然显示任意数量的滴答声!这是我的代码和结果:

import numpy as np
from bokeh.models.sources import ColumnDataSource
from bokeh.plotting import figure
from bokeh.io import show
from bokeh.palettes import Category10

p = figure(x_axis_type="datetime", plot_width=800, plot_height=500)

data = {'x': 
        [np.datetime64('2019-01-26T03:15:10'),
      np.datetime64('2019-01-26T04:15:10'),
      np.datetime64('2019-01-26T05:15:10'),
      np.datetime64('2019-01-26T06:15:10'),
      np.datetime64('2019-01-26T07:15:10')],
    'A': [10,25,15,55,40],
    'B': [60,50,80,65,120],}

source = ColumnDataSource(data=data)

cl = Category10[3][1:]


r11 = p.line(source=source, x='x', y='A', color=cl[0], line_width=3)
r12 = p.line(source=source, x='x', y='B', color=cl[1], line_width=3)


p.xaxis.formatter=DatetimeTickFormatter(
    seconds=["%H:%M:%S"],
    minsec=["%H:%M:%S"],
    minutes=["%H:%M:%S"],
    hourmin=["%H:%M:%S"],
    hours=["%H:%M:%S"],
    days=["%H:%M:%S"],
    months=["%H:%M:%S"],
    years=["%H:%M:%S"],
    )

p.y_range.start = -100
p.x_range.range_padding = 0.1
p.yaxis[0].ticker.desired_num_ticks = 5
p.xaxis.major_label_orientation = math.pi/2

show(p)

这是结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

如文档中所述,num_desired_ticks仅是一个建议。如果您希望在不改变的特定位置上打勾,则可以使用FixedTicker,为方便起见,可以通过普通列表将其设置:

p.xaxis.ticker = [2, 3.5, 4]

对于日期时间,您将传递自纪元以来的毫秒数。

如果您想要固定的刻度数,但是位置可能会发生变化(即因为范围可能会发生变化),则没有内置的方法可以执行此操作。您可以制作一个custom ticker extension