简体   繁体   English

使用散景绘制 x 轴转换 Unix 时间戳

[英]Convert Unix Timestamp with Bokeh plotting x axis

I recently reran some old code on a new install of anacondas.我最近在新安装的 anacondas 上重新运行了一些旧代码。 The code analyzes data to detect spiking in signals.该代码分析数据以检测信号中的尖峰。 When I reran the code, I recieved the error "AttributeError: unexpected attribute 'x_axis_type' to Line, possible attributes are line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, name, tags, visible, x or y"当我重新运行代码时,我收到错误"AttributeError: unexpected attribute 'x_axis_type' to Line, possible attributes are line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, name, tags, visible, x or y"错误"AttributeError: unexpected attribute 'x_axis_type' to Line, possible attributes are line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, name, tags, visible, x or y"

I did some digging and see that it is listed that bokeh now tries to automatically set the axis type for you.我做了一些挖掘,看到它列出了散景现在尝试为您自动设置轴类型。 The data I am plotting is time series values with a unix timestamp.我绘制的数据是带有 unix 时间戳的时间序列值。 I removed the attribute, the plot ran, but the axis was not formatted, it just showed the unix timestamps when I would like to see some readable dates.我删除了属性,绘图运行了,但轴没有格式化,它只是在我想查看一些可读日期时显示 unix 时间戳。 Any idea how to convert?知道如何转换吗?

Here is a snippet of my code:这是我的代码片段:

### Create plot
    p = fig(plot_width=700, plot_height = 300, title=str(i[3]) + ' | ' + str(i[4]), y_range = yr, tools="")    
    p.title.text_font_size = "10pt"
    p.line(d[0],d[1], legend=str(i[2]), line_color=c, x_axis_type="datetime")
    p.legend.orientation = "horizontal"
    show(p)

A dataframe I am plotting would looks something like this:我正在绘制的数据框看起来像这样:

d = [[  1.48280537e+12   1.48281943e+12   1.48283350e+12   1.48284756e+12
1.48286163e+12   1.48287569e+12   1.48290383e+12   1.48291790e+12
1.48293200e+12   1.48294616e+12   1.48296014e+12   1.48297422e+12
1.48298829e+12   1.48300236e+12   1.48305865e+12   1.48307271e+12
1.48308678e+12   1.48310084e+12   1.48311491e+12   1.48312901e+12
1.48314307e+12   1.48319937e+12   1.48321343e+12   1.48322750e+12
1.48324158e+12   1.48325565e+12   1.48328381e+12   1.48329789e+12
1.48335427e+12   1.48336834e+12   1.48338242e+12   1.48339650e+12
1.48341058e+12   1.48343874e+12   1.48345282e+12   1.48346695e+12
1.48348098e+12   1.48349509e+12   1.48350911e+12   1.48352318e+12
1.48353724e+12   1.48355131e+12   1.48356538e+12   1.48357946e+12
1.48359354e+12   1.48360762e+12   1.48362169e+12   1.48363576e+12
1.48364985e+12   1.48366398e+12   1.48369210e+12   1.48370617e+12
1.48373437e+12   1.48374848e+12   1.48376259e+12   1.48377672e+12
1.48379081e+12   1.48394587e+12   1.48395992e+12   1.48397399e+12
1.48403029e+12   1.48404433e+12   1.48434006e+12   1.48435411e+12
1.48436817e+12   1.48450888e+12   1.48452293e+12   1.48453698e+12
1.48455104e+12   1.48456512e+12   1.48463554e+12   1.48473398e+12
1.48474802e+12   1.48487458e+12   1.48488864e+12   1.48514210e+12
1.48515616e+12   1.48517022e+12   1.48518430e+12   1.48519835e+12
1.48521239e+12   1.48524051e+12   1.48525456e+12   1.48526863e+12
1.48528269e+12   1.48539528e+12   1.48540934e+12   1.48542340e+12
1.48543747e+12]
[  4.37600000e+01   4.37400000e+01   4.39800000e+01   4.38400000e+01
4.39000000e+01   4.38000000e+01   4.38000000e+01   3.17700000e+01
3.18800000e+01   3.19000000e+01   3.19400000e+01   3.19900000e+01
3.19900000e+01   3.21500000e+01   3.23000000e+01   3.23100000e+01
3.23800000e+01   3.24300000e+01   3.25100000e+01   3.24700000e+01
3.25000000e+01   3.27700000e+01   3.28300000e+01   3.28300000e+01
3.29000000e+01   3.29100000e+01   3.28800000e+01   3.29800000e+01
3.33200000e+01   3.34000000e+01   3.34900000e+01   3.35200000e+01
3.35500000e+01   3.37000000e+01   3.37100000e+01   3.37300000e+01
3.37600000e+01   3.37700000e+01   3.37400000e+01   3.37900000e+01
3.38300000e+01   3.38700000e+01   3.38800000e+01   3.39000000e+01
3.39300000e+01   3.39500000e+01   3.40100000e+01   3.40700000e+01
3.40600000e+01   3.41400000e+01   3.42100000e+01   3.42700000e+01
3.43200000e+01   3.43500000e+01   3.43300000e+01   3.43900000e+01
3.45100000e+01   3.49300000e+01   3.50700000e+01   3.51000000e+01
3.52500000e+01   3.52600000e+01   3.62000000e+01   3.62700000e+01
3.62800000e+01   3.66600000e+01   3.67300000e+01   3.68000000e+01
3.67800000e+01   3.70500000e+01   3.73100000e+01   3.74900000e+01
3.75200000e+01   3.79800000e+01   3.80300000e+01   3.87900000e+01
3.88600000e+01   3.88500000e+01   3.88600000e+01   3.88800000e+01
3.88700000e+01   3.89800000e+01   3.90900000e+01   3.90500000e+01
3.90600000e+01   3.94500000e+01   3.94800000e+01   3.95600000e+01
3.95900000e+01]]

And finally, this is what a plot looks like (notice the x axis):最后,这就是图的样子(注意 x 轴):

在此处输入图片说明

Any ideas on how to get the dates correct now that I can not use x_axis_type = "datetime" ?现在我不能使用x_axis_type = "datetime"任何关于如何获得正确日期的想法?

You could use NumPy to view the integers as millisecond datetime64 timestamps, then use tolist to convert the array of datetime64[ms] s to a list of datetime.datetime objects:你可以使用与NumPy查看整数作为 毫秒datetime64时间戳,然后用tolist到的数组转换datetime64[ms] s到datetime.datetime对象的列表:

x = np.array(d[0], dtype='i8').view('datetime64[ms]').tolist()

yields产量

[datetime.datetime(2016, 12, 27, 2, 22, 50),
 datetime.datetime(2016, 12, 27, 6, 17, 10),
 datetime.datetime(2016, 12, 27, 10, 11, 40),
 datetime.datetime(2016, 12, 27, 14, 6),
...
 datetime.datetime(2017, 1, 26, 1, 48),
 datetime.datetime(2017, 1, 26, 5, 42, 20),
 datetime.datetime(2017, 1, 26, 9, 36, 40),
 datetime.datetime(2017, 1, 26, 13, 31, 10)]

This is quicker than calling datetime.fromtimestamp multiple times in a loop.这比在循环中多次调用datetime.fromtimestamp更快。

In [26]: %timeit np.array(d[0], dtype='i8').view('datetime64[ms]').tolist()
100000 loops, best of 3: 14.1 µs per loop

In [27]: import datetime as DT
In [28]: %timeit [DT.datetime.fromtimestamp(ts / 1000.) for ts in d[0]]
10000 loops, best of 3: 119 µs per loop

convert the timestamp column to datetime object using the datetime.fromtimestamp() function like this (we divide by 1000 to convert milliseconds to seconds):使用datetime.fromtimestamp()函数将时间戳列转换为datetime对象,如下所示(我们除以 1000 以将毫秒转换为秒):

from datetime import datetime
timestamps = [1.48528269e+12, 1.48539528e+12, 1.48540934e+12]
dates = [datetime.fromtimestamp(ts / 1000.) for ts in timestamps]

for d in dates:
    print(d)

code output:代码输出:

2017-01-24 20:31:30
2017-01-26 03:48:00
2017-01-26 07:42:20

use dates variable to plot the data使用dates变量绘制数据

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

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