python-2.7 - Bokeh 字形坐标 x_axis_type 'datetime'

标签 python-2.7 datetime bokeh

我正在尝试将一个简单的文本字符串(字形)添加到使用 x_axis_type='datetime' 的 Bokeh 图

我的代码(只保留其要点)如下:

p = figure(plot_width=900, plot_height=380, x_axis_type='datetime')

dt  = date(2003, 3, 15)

p.line(xvals, yvals)

txt = Text(
     # x=some_formatting_function(dt), 
     x=1057005600000, 
     y=0.1, 
     text=["happy day!"],
     text_align="left", 
     text_baseline="middle",
     text_font_size="11pt", 
     text_font_style="italic", 
 )
p.add_glyph(txt)
show(p)

x 轴范围/值(即日期)从 2002 年到 2006 年运行,我想在 2003 年添加文本。我在上面的代码中显示的 x 值(即 1057005600000 --这是我通过反复试验得出的)将字形放在正确的位置。

但我不知道如何直接使用 datetime.date...

是否有 Bokeh 函数(或 datetime.date 的属性)会给我 Bokeh 图所期望的值?

非常感谢。

注意我试过使用 x = bokeh.properties.Date(dt) 但这给了我:

ValueError: expected an element of either String, 
Dict(String, Either(String, Float)) or Float, got <bokeh.properties.Date object 

最佳答案

当 x_axis_type 属性设置为“datetime”时,Bokeh 将根据自纪元以来的秒数沿 x 轴绘制内容。最简单的解决方案是使用 datetime.datetime(不是 .date),然后使用 timestamp() 方法(这将给出您得到的 ~1.50e9 数字)将您的 dt 对象转换为 seconds-since-epoch,然后将其用于你的 x 坐标。

$ from datetime import datetime
$ dt = datetime.now()
$ dt
> datetime.datetime(2015, 6, 17, 10, 41, 34, 617709)
$ dt.timestamp()
> 1434555694.617709

关于python-2.7 - Bokeh 字形坐标 x_axis_type 'datetime',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30892252/

相关文章:

python - 使用 Holoviews,如何设置标题?

python - 如何将简单的 png 图片添加到我的 Bokeh 网站?

python - 数据帧 : Drop Down menu to select columns to Display (Bokeh) 的交互图

python - Pandas - 获取与两个数据帧之间的 url 匹配的模式

python - 使用 python 子进程将文件从 .sam 转换为 .bam

C# 类似于 C++ 中的 DateTime

datetime - AttributeError: 'datetime.timedelta' 对象没有属性 '_get_object_id' : pyspark

Python:填充空心物体

python - Pandas - 基于 bool DataFrame 替换 DataFrame 中的值

php - DateTime 对象上的不同 timezone_types