I'm working in Python 2.7, and I've got timestamps and corresponding values. I want to set these values to a time base of one value for each second, namely, the last measured value. So:
[[1, 4, 6],
[15, 17, 12]]
to:
[[1, 2, 3, 4, 4, 6],
[15, 15, 15, 17, 17, 12]]
I've come up with this, which does what I want, but there must a more elegant way. Does anyone know of one?
import numpy as np
#Example data:
origdata= {}
origdata['time'] = [4, 26, 37, 51, 59, 71, 93]
origdata['vals'] = [17, 5, 43, 21, 14, 8, np.NaN]
extratime = [t-1 for t in origdata['time']]
data={}
data['time'] = np.concatenate((origdata['time'][:-1], extratime[1:]), axis=0)
data['vals'] = np.concatenate((origdata['vals'][:-1], origdata['vals'][:-1]), axis=0)
sorter = data['time'].argsort()
data['time'] = data['time'][sorter]
data['vals'] = data['vals'][sorter]
filledOutData = {}
filledOutData['time'] = range(data['time'][0], data['time'][-1])
filledOutData['vals'] = np.interp(filledOutData['time'], data['time'], data['vals'])
Plotting the original data and desired result with the following code gives the image below:
import matplotlib.pyplot as plt
plt.plot(origdata['time'], origdata['vals'], '-o', filledOutData['time'], filledOutData['vals'], '.-')
plt.legend(['original', 'desired result'])
plt.show
