Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

unable to recreate pandas's bar plot in seaborn

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

enter image description here

i tried melting the df but the results are the same

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading