简体   繁体   English

如何将日期时间轴与 Bokeh image_rgba 元素一起使用?

[英]How to use a datetime axis with a Bokeh image_rgba element?

I have a situation where I would like to generate a Bokeh image_rgba plot with datetime axes.我有一种情况,我想用日期时间轴生成 Bokeh image_rgba 图。 When I try this, with something similar to the following code:当我尝试此操作时,使用类似于以下代码的内容:

p1 = figure(x_axis_type = 'datetime', y_axis_type = 'datetime',
           x_range = [min(dates),max(dates)], 
           y_range = [min(dates),max(dates)],
           x_axis_label = 'Purchase Date', y_axis_label = 'Sell Date', 
           plot_width = 500, plot_height = 500)
p1.image_rgba(image = [plotmatrix], x = [min(dates)], y = [min(dates)], dh = [max(dates)-min(dates)], dw = [max(dates)-min(dates)])
p1.grid.grid_line_color = None
show(p1)

I get the following error:我收到以下错误:

TypeError: Timedelta('2139 days 00:00:00') is not JSON serializable

The plotmatrix variable is a square numpy matrix with a data type of uint32 which has been packed with RGBA values based on the example at plotmatrix变量是一个方形 numpy 矩阵,其数据类型为 uint32,它已根据示例中的 RGBA 值打包

http://docs.bokeh.org/en/latest/docs/gallery/image_rgba.html .http://docs.bokeh.org/en/latest/docs/gallery/image_rgba.html

What is the recommended way to address this.解决这个问题的推荐方法是什么。 I realize that one option for me would be to cast all of the times into unixtime, plot the data based on seconds and then find some sort of axis label formatter to produce the correct time information.我意识到我的一个选择是将所有时间都转换为 unixtime,基于秒绘制数据,然后找到某种轴标签格式化程序来生成正确的时间信息。 But something about that solution seems a bit too hackish for me.但是关于这个解决方案的某些东西对我来说似乎有点太hackish了。

The non-serializability of TimeDelta was recently addressed in PR #8027 Handle Pandas PeriodIndex and Timedelta properly .最近在PR #8027处理 Pandas PeriodIndex 和 Timedelta 中解决了TimeDelta的不可序列化问题。 As of July 2018 this fix is not yet included in any official release.截至 2018 年 7 月,此修复程序尚未包含在任何正式版本中。 It will be part of the upcoming 1.0 release in Q3 2018.它将成为 2018 年第三季度即将发布的 1.0 版本的一部分。

Alternatively, if you want to convert timedelta values directy by hand to a format that Bokeh can always understand, the following function (which is all the PR above does) will work:或者,如果您想手动将 timedelta 值直接转换为 Bokeh 始终可以理解的格式,则以下函数(即上述 PR 所做的全部)将起作用:

def convert_timedelta_type(obj):
    ''' Convert any recognized timedelta value to floating point absolute milliseconds.

    Arg:
        obj (object) : the object to convert
    Returns:
        float : milliseconds

    '''
    if isinstance(obj, dt.timedelta):
        return obj.total_seconds() * 1000.
    elif isinstance(obj, np.timedelta64):
        return (obj / np.timedelta64(1, 'ms'))

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

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