2014-10-06 73 views
1

我的数组有一对unix时间戳和值。通过时间戳绘制一个numpy数组的直方图

[[ 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]] 

我设法绘制了整个第二列的直方图pyplot.hist(array[:,1]); pyplot.show()。但是我真正想要做的是按日期分组array[:,1](由array [:,0]中的unix时间戳导出),并将它们绘制为堆积直方图,每个(有色)堆栈代表一天。什么可能是最好的方式来做到这一点?

回答

1

既然你参与了这个groupby,它才有意义使用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

,你也为了避免需要将它们按年 - 月 - 日将不同月份/年份的天数分组在一起。