我的数组有一对unix时间戳和值。
[[ 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()
绘制整个第二列的直方图。但我真正想做的是,按日{bin array[:,1]
(由数组[:,0]中的unix时间戳导出),并将它们绘制为堆叠直方图,每个(彩色)堆栈代表一个天。什么是最好的方法呢?
答案 0 :(得分: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>
此外,您还需要按年度分组,以避免将不同月份/年份的日子组合在一起。