如何在Seaborn中将1行数据框绘制为条形图?

时间:2018-11-13 18:15:19

标签: python pandas dataframe bar-chart seaborn

我不明白,它想要从数据框中绘制什么。我有一个数据框

enter image description here

并尝试绘制它。目标是每个栏有一个酒吧。但是我不能。

我尝试了各种组合:

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

2 个答案:

答案 0 :(得分:1)

尝试使用melt函数在熊猫中将此宽格式更改为长格式。

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

答案 1 :(得分:0)

使用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)

数据的唯一问题是,您需要对其进行展平以使其兼容并使用上述barplot参数