【问题标题】:Bokeh time series plot annotation is off by 1 hour散景时间序列图注释关闭 1 小时
【发布时间】:2017-01-16 11:10:07
【问题描述】:

我正在绘制带有日期时间 X 轴的散景图。在绘图中添加注释时,我注意到时间是一小时。我怀疑这是因为我处于 UTC+1 时区,尽管它也可能在某个地方存在一些 +1 索引差异。

要重现的代码:

xrange = pandas.date_range('1/1/2011', periods=12, freq='H')
event = pandas.Timestamp('1/1/2011 05:00:00')
data = pandas.Series([1]*12, index=xrange)
data[event] = 3

plot = bokeh.plotting.figure(x_axis_type="datetime")
plot.line(data.index, data)

time = event.timestamp()*1000
spanannotation = bokeh.models.Span(location=time, dimension="height",line_color="red")

plot.renderers.append(spanannotation)
bokeh.plotting.show(plot)

输出:

如何让注释显示在正确的时间?

编辑:它肯定与时区有关,因为当我将系统时区更改为 UTC+2 时,偏移量为 2 小时。

【问题讨论】:

    标签: python pandas plot timestamp bokeh


    【解决方案1】:

    这是这个问题https://github.com/bokeh/bokeh/issues/5499

    Bokeh 会将您的 datetime 对象视为系统本地时间。您可以在代码开头使用这些行来阻止它,以使您的系统时间为 UTC+0:

    import os
    import time    
    
    os.environ['TZ'] = 'UTC+0'
    time.tzset()
    

    【讨论】:

      【解决方案2】:

      @Seb 是正确的 - Bokeh 假定“原始”时间戳(没有指定时区的时间)在系统时钟的时区中。解决此问题的另一种方法是为您的 pandas Timestamp 定义时区。为此,您可以tz_localize 联系我们。

      下面是您的代码:

      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)
      

      另请注意,Span 对象的 Bokeh 参考建议使用

      time.mktime(dt(2013, 3, 31, 2, 0, 0).timetuple())*1000
      

      将 datetime 对象转换为 UNIX 时间,但 timetuple() 会删除所有时区信息,因此在我看来,您使用 timestamp() 会更好:-)

      【讨论】:

        猜你喜欢
        • 2017-03-31
        • 1970-01-01
        • 2012-06-19
        • 2014-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-13
        • 1970-01-01
        相关资源
        最近更新 更多