Bokeh Time Series Plot Annotation Is Off By 1 Hour
I am plotting a Bokeh plot with a datetime X-axis. When adding an annotation to the plot I notice that the time is one hour off. I suspect this is due to me being in an UTC+1 time-
Solution 1:
This is this issue https://github.com/bokeh/bokeh/issues/5499
Bokeh will treat your datetime objects as system local time. You can prevent it with those lines at the beginning of your code to have your system time in UTC+0:
import os
import timeos.environ['TZ'] = 'UTC+0'time.tzset()
Solution 2:
@Seb is correct—Bokeh assumes that "naive" timestamps (times with no specified timezone) are in the system clock's timezone. Another way to solve this problem is to define the timezone for your pandas Timestamp. You can us tz_localize for this.
Here would be your code below:
import pandas
from bokeh.plotting import figure, show
from bokeh.models import Span
xrange = pandas.date_range('1/1/2011', periods=12, freq='H')
event = pandas.Timestamp('1/1/2011 05:00:00').tz_localize('UTC')
data = pandas.Series([1]*12, index=xrange)
data[event] = 3
plot = figure(x_axis_type="datetime")
plot.line(data.index, data)
time = event.timestamp()*1000
spanannotation = Span(location=time, dimension="height",line_color="red")
plot.renderers.append(spanannotation)
show(plot)
Also note that the Bokeh reference for the Span object suggests using
time.mktime(dt(2013, 3, 31, 2, 0, 0).timetuple())*1000
to convert datetime objects to UNIX time, but timetuple() removes all timezone info, so your use of timestamp() is way better, in my opinion :-)
Post a Comment for "Bokeh Time Series Plot Annotation Is Off By 1 Hour"