Bokeh 中的日期时间轴

问题描述 投票:0回答:2

对于我的图,我想在散景中使用“日期时间”选项,如下所示:

top = figure(width=900, height=500, x_axis_type='datetime')

我的 x 轴数据采用 datetime.time 格式。

x_time = [datetime.time(0, 0), datetime.time(0, 0, 3), datetime.time(0, 0, 13), datetime.time(0, 0, 23), datetime.time(0, 0, 26)]

但是在尝试添加时会产生以下错误:

top.image_url(x=datetime.time(0,0,3), y= 10 url = [some_url]]

top.add_layout(Arrow(x_start=datetime.time(0,0,0), y_start=5,
            x_end=datetime.time(0,0,3), y_end=10)


ValueError: expected an element of either String, Dict(String, Either(String, Instance(Transform), Instance(ColorMapper), Float)) or Float, got datetime.time(0, 0)

根据 Rutger Kassies 的建议,我将数据转换为微秒,现在它只显示秒: Change from seconds to minutes

datetime bokeh
2个回答
3
投票

至少从 bokeh 0.12.5 开始,如果您的 x 轴包含日期时间格式的数据,您可以简单地将其声明为figure函数中的参数,并且将为您完成格式化:

plot=figure(plot_height=300, plot_width=800,x_axis_type="datetime")

2
投票

Bokeh 注释似乎只接受数字,而不是

Datetime
Time
对象。解决方法是将您的时间转换为微秒并使用它们来绘图。

一个例子:

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import Arrow
import datetime

def time_to_microseconds(t):
    dmin = datetime.datetime.min
    dummy_tdelta = (datetime.datetime.combine(dmin, t) - dmin)
    return dummy_tdelta.total_seconds()*1000

x_time = [datetime.time(0,0,1),
          datetime.time(0,0,2),
          datetime.time(0,0,3),
          datetime.time(0,0,4),
          datetime.time(0,0,5)]

top = figure(width=300, height=300, x_axis_type='datetime')

# a line works fine with time objects
top.line(x_time, range(len(x_time)))

# layout needs numbers
top.add_layout(Arrow(x_start=time_to_microseconds(datetime.time(0,0,2)), 
                     y_start=3,
                     x_end=time_to_microseconds(datetime.time(0,0,3)), 
                     y_end=2))

编辑:

您可以使用以下方法更改刻度格式:

from bokeh.models import DatetimeTickFormatter

top.xaxis.formatter = DatetimeTickFormatter(seconds=["%M:%S"],
                                            minutes=["%M:%S"],
                                            minsec=["%M:%S"],
                                            hours=["%M:%S"])
© www.soinside.com 2019 - 2024. All rights reserved.