2017-04-05 96 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

相关问题