unable to recreate pandas's bar plot in seaborn

Question:

I can easily create the bar plot in pandas as below:

import pandas as pd 

revenue = [100, 110, 105, 120, 95]
profit = [35, 50, 45, 65, 30]
quarter = ['Q2/22', 'Q1/22', 'Q4/21', 'Q3/21', 'Q2/21']

df = pd.DataFrame({'Revenue': revenue, 'Profit': profit}, index=quarter)

df.plot(kind='bar') 

enter image description here

when I try to plot the same df in seaborn it gives me different graph:

import seaborn as sns
sns.barplot(data=df)

enter image description here

i tried melting the df but the results are the same

Asked By: A.E

||

Answers:

Using melt and then barplot with hue:

temp = (df.melt(ignore_index=False, var_name='category')
            .reset_index().rename(columns={'index': 'quarter'}))
sns.barplot(x='quarter', y='value', hue='category', data=temp)

Output:

enter image description here

Answered By: BigBen
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.