Dims
Dims

Reputation: 51039

How to plot 1-row dataframe as a barplot in seaborn?

I don't understand, what it wants from dataframe to be plotted. I have a dataframe

enter image description here

and trying to plot it. The goal is to have one bar per colum. But I can't.

I tried various combinations of this:

#df = df.transpose()
sns.barplot(y=df.index.values, x=df.values, order=df.index)

Upvotes: 0

Views: 4257

Answers (2)

anuj bajpai
anuj bajpai

Reputation: 87

Plot using seaborn.barplot

    import pandas as pd
    import seaborn as sns
    sales = {'Tony': 103,
     'Sally': 202,
     'Randy': 380,
     'Ellen': 101,
     'Fred': 82
    }

    #making dict to 1 row DataFrame
    df = pd.DataFrame(sales, index=[0])

    #flattening the values to be compliant with 
    #barplot method of seaborn and columns of dataframe
    
    values = df.values.flatten()
    sns.barplot(x = df.columns,y=values)

The only problem with your data is just you need to flatten it to make it compatible and use the above arguments of barplot

Upvotes: 2

Naveen
Naveen

Reputation: 1210

Try to change this wide format to long format using melt function in pandas.

long_df = pd.melt(df)
sns.barplot(y = long_df.variable, x = long_df.value)

Upvotes: 2

Related Questions