Reputation: 101
I have datafrme on which I perform groupby on two columns:
ax = df_all.groupby(['Component', 'SubComponent' ])['SubComponent'].count()
And this give me this:
--------------------------------------------------
| Component | SubComponent| |
--------------------------------------------------
|A |First | 1|
--------------------------------------------------
| |Second | 10|
--------------------------------------------------
| |Third | 6|
--------------------------------------------------
|B |First | 8|
--------------------------------------------------
| |Second | 5|
--------------------------------------------------
| |Third | 17|
--------------------------------------------------
Now I would like to make a histogram out of it. Each bar would be Component and each component would divided on SubComponents showing number of occurrence of each SubComponent with different color.
Is there easy way to accomplish it with matplotlib.pyploy ?
Thanks, Submi
Upvotes: 3
Views: 2771
Reputation: 862611
You can use unstack
with 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)
print (ax.unstack(0))
Component A B
SubComponent
First 1 6
Second 4 2
Third 15 1
ax.unstack(0).plot.bar(stacked=True)
Notice:
What is the difference between size and count in pandas?
Upvotes: 3