将数据框绘制为条形图,每列都在单独的y轴上

时间:2020-10-15 14:27:11

标签: python pandas dataframe matplotlib

我有一个这样的数据框:

enter image description here

我想找到一种在matplotlib条形图中显示此内容的简单方法,其中各列分别是yaxis。如下图所示:

enter image description here

2 个答案:

答案 0 :(得分:1)

使用seaborn并使用hue参数:

fig, ax = pyplot.subplots(figsize=(10, 10))
ax =seaborn.barplot(
    data= df.melt(id_vars='index').rename(columns=str.title),
    x= 'index',
    y= 'value',
    hue='varaible'
)

答案 1 :(得分:0)

让我们尝试一下:

# sample data
df = pd.DataFrame({'sum_prices':[100,200,300],
                  'properties':[10,5, 4]}, index=list('abc'))

df.plot.bar(secondary_y=['properties'])

输出:

enter image description here