我有一个数据帧,它看起来像:Python的 - 绘制一周明智分布
date
1 2008-10-05
10 2007-03-30
100 2008-07-05
1000 2007-03-30
1001 2014-08-14
1002 2007-09-30
1003 2007-06-14
1004 2006-01-13
1005 2006-08-04
1006 2007-06-14
1007 2008-11-30
我想要做的就是情节的柱状图,显示的日期分布缩减到一个星期。 例如, 该索引是一个图片ID,我想深入了解2006年10月第一周拍摄的照片数量。换句话说,我需要每周的直方图。
df['week'].iplot(kind='histogram')
只给出了周数,但我想将其与年份相关联。
如果有人能够帮助我使用plot.ly来绘制直方图,那将会很棒。 matplotlib图也可以。
感谢您的帮助。
编辑:下面是最终解决我的问题:
df_new = df.groupby(['year','week']).count()['date']
df_dict = df_new.to_dict()
df_tups = [(' wk#'.join(map(str,key)), df_dict[key]) for key in df_dict.keys()]
df_tups = sorted(df_tups, key=lambda x : (x[0], x[1]))
x = ["'"+tup[0][2:] for tup in df_tups]
y = [tup[1] for tup in df_tups]
trace1 = go.Bar(
x = x,
y = y
)
data = [trace1]
layout = go.Layout(
xaxis=dict(tickangle=45)
)
fig = dict(data=data, layout=layout)
py.iplot(fig)
你能列名添加到您的数据帧? –
当然,绝对。 @MaximilianPeters –
在您的示例数据中没有“星期”栏或我错过了什么? –