John Doe
John Doe

Reputation: 67

How to plot this type of graph in Python Pandas

I have a data-frame like this:

Hour     Type       Value
1          1          63
1          2          52.1
1          5          53.8
1          4          92.1
1          3          1.4
1          6          4.1
1          2          0
2          1          8.1
2          2          6.1
2          6          0.6
2          5          0.1
2          4          1.09
3          1          9.5
...       ...         ...
23         3          22.5

I want to plot a graph like this

enter image description here

Let me explain:

I want to calculate the mean by per hour, this can be easily done by

meanValuePerHour= df.groupby('hour')['Value'].agg('mean')

rpmByHour.plot.bar(x='Hour', y='Mean Value',title ='Mean Valueby Hour');

But, Now, I want to add the Type variable. Do you see different colors in each bar? That is how I want to also groupby Type.

Something like this df.groupby(['Hour','Type'])['Value'].agg('mean')

Upvotes: 0

Views: 67

Answers (1)

Chris
Chris

Reputation: 29742

IIUC, pandas.DataFrame.unstack with plot(kind='bar', stacked=True) is what you want:

df.groupby(['Hour', 'Type'])['Value'].mean().unstack().plot(kind='bar', stacked=True)

Output:

enter image description here

Upvotes: 2

Related Questions