我最近在新安装的anacondas上重新编写了一些旧代码。代码分析数据以检测信号中的尖峰。当我重新编写代码时,我收到了错误"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"
我做了一些挖掘,看到它列出了散景现在尝试为你自动设置轴类型。我正在绘制的数据是具有unix时间戳的时间序列值。我删除了属性,绘图运行,但轴没有格式化,它只显示了unix时间戳,当我想看到一些可读日期。知道怎么转换?
以下是我的代码片段:
### 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)
我正在绘制的数据框看起来像这样:
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]]
最后,这就是情节的样子(注意x轴):
Creating Event Handlers in Visual Studio
现在我无法使用x_axis_type = "datetime"
?
答案 0 :(得分:2)
您可以使用NumPy 查看整数作为millisecond datetime64时间戳,然后使用tolist
将datetime64[ms]
的数组转换为日期时间列表。日期时间对象:
x = np.array(d[0], dtype='i8').view('datetime64[ms]').tolist()
产量
[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)]
这比在循环中多次调用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
答案 1 :(得分:0)
使用datetime
函数将timestamp列转换为datetime.fromtimestamp()
对象(我们除以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)
代码输出:
2017-01-24 20:31:30
2017-01-26 03:48:00
2017-01-26 07:42:20
使用dates
变量绘制数据