Aditya Kuls
Aditya Kuls

Reputation: 105

Generating stacked histogram for same row entries in 2 different dataframes using matplotlib in python

I have 2 dataframes :

 Opposition    Result   GameType    Count
 Afghanistan    won      Set         1
 Australia      won      Set         10
 Bangladesh     won      Set         15
 Bermuda        won      Set         1
 Canada         won      Set         1
 England        won      Set         15
 India          won      Set         17
 Ireland        won      Set         3
 Kenya          won      Set         1
 Netherlands    won      Set         3
 New Zealand    won      Set         13
 Pakistan       won      Set         21
 Scotland       won      Set         2
 South Africa   won      Set         14
 U.A.E.         won      Set         2
 West Indies    won      Set         9
 Zimbabwe       won      Set         12

And

Opposition    Result GameType  Count
Afghanistan    won    Chase      1
Australia      won    Chase      9
Bangladesh     won    Chase     14
Canada         won    Chase      1
England        won    Chase     12
India          won    Chase     15
Ireland        won    Chase      1
Kenya          won    Chase      1
New Zealand    won    Chase     15
Pakistan       won    Chase     13
South Africa   won    Chase      9
West Indies    won    Chase      7
Zimbabwe       won    Chase     16

I want to create a stacked histogram for these countries(3 extra in 1 DF) split by GameType(chase or set).

I tried this :

pyplot.hist([SriGroupWinChase['Count'],SriGroupWinSet['Count']],stacked=True, color = ['r','g'])

But this is giving some weird result of histogram.

What is the right way to do it? Also, how can i stylise my histogram more?

Upvotes: 0

Views: 225

Answers (1)

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

Instead of histogram you need to use bar plot to represent the data. Concat the dataframes and use pivot table so that missing data will be filled with NaN and then plot the table.

df=pd.concat([df1,df2])
table = pd.pivot_table(df, values='Count', index=['Opposition'], columns=['GameType'], aggfunc=np.sum)

table.plot(kind="bar",color=['r','b'])

Output of table :

GameType     Chase   Set
Opposition              
Afghanistan    1.0   1.0
Australia      9.0  10.0
Bangladesh    14.0  15.0
Bermuda        NaN   1.0
Canada         1.0   1.0
England       12.0  15.0
India         15.0  17.0
Ireland        1.0   3.0
Kenya          1.0   1.0
Netherlands    NaN   3.0

Output of the plot :

enter image description here

If you want stacked bars, you can use stacked=True ie.

table.plot(kind="bar",color=['r','b'],stacked=True)

Output: enter image description here

Hope it helps.

Upvotes: 1

Related Questions