我最近在新安装的 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 "
我做了一些挖掘,发现其中列出了 bokeh 现在尝试自动为您设置轴类型。我正在绘制的数据是带有 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 轴):
现在我不能使用 x_axis_type = "datetime"
,关于如何正确获取日期有什么想法吗?
请您参考如下方法:
您可以使用 NumPy 将整数查看为 millisecond datetime64时间戳,然后使用 tolist
将 datetime64[ms]
数组转换为 datetime.datetime 对象列表:
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