我有一個數據幀,它看起來像: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 –
在您的示例數據中沒有「星期」欄或我錯過了什麼? –