aag
aag

Reputation: 730

plotting a histogram of a numpy array by timestamp

My array has pairs of unix timestamps and values.

[[  1.40170249e+09   9.00000000e+01]
 [  1.40170249e+09   9.10000000e+01]
 [  1.40170249e+09   9.20000000e+01]
 ..., 
 [  1.41149703e+09   1.09000000e+02]
 [  1.41149703e+09   1.06000000e+02]
 [  1.41149703e+09   1.06000000e+02]]

I have managed to plot a histogram of the whole second column with pyplot.hist(array[:,1]); pyplot.show(). But what I really want to do, is to bin array[:,1] by day (as derived by the unix timestamps in array[:,0]), and plot these as a stacked histogram, with each (colored) stack representing a day. What might be the best way to do that?

Upvotes: 1

Views: 1248

Answers (1)

CT Zhu
CT Zhu

Reputation: 54340

Since you have a groupby involved in this, it will make sense to use pandas:

In [192]:
import pandas as pd
import numpy as np
import time
A = np.array([[  1.40170249e+09,   9.00000000e+01],
             [  1.40170249e+09,   9.10000000e+01],
             [  1.40170249e+09,   9.20000000e+01],
             [  1.41149703e+09,   1.09000000e+02],
             [  1.41149703e+09,   1.06000000e+02],
             [  1.41149703e+09,   1.06000000e+02]])
df = pd.DataFrame(A, columns=['date', 'val'])
df['date'] = df.date.map(lambda x: time.gmtime(x))
print df
                                   date  val
0    (2014, 6, 2, 9, 48, 10, 0, 153, 0)   90
1    (2014, 6, 2, 9, 48, 10, 0, 153, 0)   91
2    (2014, 6, 2, 9, 48, 10, 0, 153, 0)   92
3  (2014, 9, 23, 18, 30, 30, 1, 266, 0)  109
4  (2014, 9, 23, 18, 30, 30, 1, 266, 0)  106
5  (2014, 9, 23, 18, 30, 30, 1, 266, 0)  106
In [193]:

grp_obj = df.groupby(df.date.map(lambda x: time.strftime('%Y-%m-%d', x)))
plt.hist([value.val.values for grp, value in grp_obj],
         stacked=True, 
         label=[grp for grp, value in grp_obj])
plt.legend()
Out[193]:
<matplotlib.legend.Legend at 0x10902d950>

enter image description here

And also you need to group them by year-month-day in order to avoid having days in different months/years grouped together.

Upvotes: 1

Related Questions