2017-04-05 98 views
1

我試圖在條形圖中每次顯示2個條形圖。 X軸必須是時間,y軸必須被計數。 每次我試圖爲每個id繪製2個小節。使用matplotlib爲每個x軸值添加多個條形圖

Id Time Count 
    585 10 9 
    585 11 34 
    585 12 96 
    193 10 147 
    193 11 85 
    193 12 1 

嘗試使用下面的代碼,但無法獲得理想的結果。任何幫助都會非常有幫助。

在此先感謝!

y = df1['Id'] 
    z = df1['Count'] 

    ax = plt.subplot(111) 
    ax.bar(y, z,width=0.2,color='b',align='center') 
    ax.bar(y, z,width=0.2,color='g',align='center') 
    ax.xaxis_date() 

    plt.show() 

回答

1
​​

+ unstack

df.set_index(['Time', 'Id']).Count.unstack().plot.bar() 

enter image description here

此時間Count作爲ylabel

ax = df.set_index(['Time', 'Id']).Count.unstack().plot.bar() 
ax.set_ylabel('Count') 

enter image description here

+0

非常感謝! – user3447653

+0

@ user3447653非常歡迎您! – piRSquared

相關問題