Bokeh coordinates with x_axis_type 'datetime'

I am trying to add a simple text string (glyph) to a Bokeh graphic that uses x_axis_type='datetime'

My code (stripped of its essence) looks like this:

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)

      

The range / values ​​on the x-axis (i.e. dates) start from 2002 to 2006 and I would like to add text in, say, 2003. The x value I specified in the above code (i.e. 1057005600000 - which I developed by trial and error) drops the glyph in the right place.

But I cannot work out how to use datetime.date directly ...

Is there a bokeh function (or datetime.date property) that will give me the value the bokeh plot expects?

Many thanks.

NB I tried using x = bokeh.properties.Date(dt)

, but this gives me:

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

      

+3


source to share


2 answers


See the following SO question / answer for python2's answer to my problem:

How can I convert a datetime object to milliseconds from epoch (unix time) in Python?



Thanks @Luke Canavan for pointing me in the right direction (!)

0


source


When x_axis_type attr is set to "datetime", Bokeh will render objects along the x-axis according to the second-s-epoch. The simplest solution is to use datetime.datetime (not.date) and then apply your dt object to epoch seconds using the timestamp () method (which will give the ~ 1.50e9 number you get) then use that for your x coordinate ...



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

      

+2


source







All Articles