Pandas组的直方图与matplotlib

时间:2017-04-21 09:36:39

标签: python pandas matplotlib

我有datafrme,我在两列上执行groupby:

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

1 个答案:

答案 0 :(得分:2)

您可以unstack使用DataFrame.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?