散景中的日期时间轴 [英] Datetime axis in Bokeh

查看:76
本文介绍了散景中的日期时间轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的身材,我想在bokeh中使用"datetime"选项,如下所示:

for my figure I wanted to use the 'datetime' option in bokeh as following:

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

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

My data for the x-axis is in the datetime.time format.

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)]

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

However it produces the following error when trying to add:

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)的建议,我将数据转换为微秒,现在仅显示秒: 从秒更改为分钟

As suggested from Rutger Kassies I transformed my data to microseconds, now it only shows seconds: Change from seconds to minutes

推荐答案

似乎Bokeh批注仅包含数字,而不包含DatetimeTime对象.一种解决方法是将您的时间转换为微秒,然后使用这些时间进行绘图.

It seems the Bokeh annotations only take numbers, not Datetime or Time objects. A workaround is to convert your times to microseconds and use those to plot.

一个例子:

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))

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

You can change the tick-formatting with:

from bokeh.models import DatetimeTickFormatter

top.xaxis.formatter = DatetimeTickFormatter(seconds=["%M:%S"],
                                            minutes=["%M:%S"],
                                            minsec=["%M:%S"],
                                            hours=["%M:%S"])

这篇关于散景中的日期时间轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆