I recently reran some old code on a new install of 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"
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. 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. 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):
Any ideas on how to get the dates correct now that I can not use 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:
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.
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):
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.