2017-04-21 121 views
3

熊猫GROUPBY我有datafrme上,我的两列执行GROUPBY:直方图与matplotlib

ax = df_all.groupby(['Component', 'SubComponent' ])['SubComponent'].count() 

这给我这个:

-------------------------------------------------- 
| Component | SubComponent|      | 
-------------------------------------------------- 
|A   |First  |      1| 
-------------------------------------------------- 
|   |Second  |     10| 
-------------------------------------------------- 
|   |Third  |      6| 
-------------------------------------------------- 
|B   |First  |      8| 
-------------------------------------------------- 
|   |Second  |      5| 
-------------------------------------------------- 
|   |Third  |     17| 
-------------------------------------------------- 

现在我想作一个直方图出的。 每个条都是Component,每个组件将在SubComponents上进行划分,显示每个SubComponent具有不同颜色的出现次数。

是否有简单的方法来完成它与matplotlib.pyploy?

感谢, Submi

回答

2

您可以使用unstackDataFrame.plot.bar

print (ax.unstack()) 
SubComponent First Second Third 
Component       
A     1  4  15 
B     6  2  1 

ax.unstack().plot.bar(stacked=True) 

graph


print (ax.unstack(0)) 
Component  A B 
SubComponent  
First   1 6 
Second   4 2 
Third   15 1 

ax.unstack(0).plot.bar(stacked=True) 

graph3

通知

What is the difference between size and count in pandas?

+0

谢谢,这似乎很好地工作。 – Submi

+0

你可能知道为什么我无法使用plt.figure(figsize =(10,8))更改此图的大小吗? – Submi

+1

尝试'ax.unstack()。plot.bar(stacked = True,figsize =(10,8))' – jezrael