user2512443
user2512443

Reputation: 473

Bar plot in Pandas from several dataframes

Suppose I have three dataframes with the same index and same columns names: df1, df2, df3.

df1

Hour | X1 | X2 | X3 | X4
 0   | 15 | 13 | 25 | 37  
 1   | 26 | 52 | 21 | 45 
 2   | 18 | 45 | 45 | 25 
 3   | 65 | 38 | 98 | 14

df2

Hour | X1 | X2 | X3 | X4
 0   | 10 | 13 | 45 | 37  
 1   | 20 | 53 | 31 | 45 
 2   | 13 | 43 | 45 | 25 
 3   | 65 | 32 | 38 | 14

df3

Hour | X1 | X2 | X3 | X4
 0   | 11 | 13 | 25 | 37  
 1   | 21 | 52 | 21 | 45 
 2   | 18 | 41 | 45 | 25 
 3   | 65 | 31 | 98 | 14

Hour is my index. I want to create a barplot. The x-axis is the Hour index and Y-axis is X1 values from each dataframe and I have legends "df1", "df2", and "df3".

A generic format of the plot should be something like enter image description here

Upvotes: 2

Views: 187

Answers (1)

BENY
BENY

Reputation: 323226

Doing with concat + unstack

pd.concat([df1,df2,df3],keys=[1,2,3]).unstack(0).plot(kind='bar')

enter image description here

Upvotes: 2

Related Questions