【问题标题】:Datetime axis in Bokeh散景中的日期时间轴
【发布时间】:2020-11-23 02:22:44
【问题描述】:

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

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


    【解决方案1】:

    Bokeh 注释似乎只接受数字,而不是 DatetimeTime 对象。一种解决方法是将您的时间转换为微秒并使用它们进行绘图。

    一个例子:

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

    【讨论】:

    • 这似乎是一个疏忽,应该很容易解决。我鼓励您在github.com/bokeh/bokeh/issues 提出问题
    • nm 下一个版本已经有一个问题:github.com/bokeh/bokeh/issues/5831 它的标题令人困惑。
    • 谢谢!这可以持续几秒钟,但是当我到达分钟时,它会从 0 开始。我在初始问题中添加了一张图片。
    • 看起来不错,它的 Bokeh 的默认刻度格式导致它,我已经更新了显示如何自定义格式的答案。
    【解决方案2】:

    不确定问题的发布者使用的是哪个版本的散景,但现在,如果您有一个带有日期时间格式数据的 x 轴,您可以简单地将其声明为图形函数中的参数,格式将为你完成:

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

    在此处查看示例: https://docs.bokeh.org/en/latest/docs/gallery/range_tool.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多