pandas seaborn 多变量组条形图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49505456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:22:20  来源:igfitidea点击:

seaborn multiple variables group bar plot

pandasmatplotlibseaborndata-analysis

提问by jayko03

I have pandas dataframe, one index(datetime) and three variables(int)

我有Pandas数据框,一个索引(日期时间)和三个变量(整数)

date        A   B       C
2017-09-05  25  261     31
2017-09-06  261 1519    151
2017-09-07  188 1545    144
2017-09-08  200 2110    232
2017-09-09  292 2391    325

I can create grouped bar plot with basic pandas plot.

我可以使用基本的Pandas图创建分组条形图。

df.plot(kind='bar', legend=False)

enter image description here

在此处输入图片说明

However, I want to display in Seaborn or other libraries to improve my skills.
I found very close answer(Pandas: how to draw a bar plot with two categories and four series each?).
In its suggested answer, it has code that

但是,我想在 Seaborn 或其他库中展示以提高我的技能。
我找到了非常接近的答案(Pandas:如何绘制具有两个类别和四个系列的条形图?)。
在其建议的答案中,它的代码是

ax=sns.barplot(x='', y='', hue='', data=data)

If I apply this code to mine, I do not know what my 'y` would be.

如果我将此代码应用于我的,我不知道我的“y”会是什么。

ax=sns.barplot(x='date', y=??, hue=??, data=data)

How can I plot multiple variables with Seaborn or other libraries?

如何使用 Seaborn 或其他库绘制多个变量?

回答by jezrael

I think need meltif want use barplot:

我认为需要melt如果想使用barplot

data = df.melt('date', var_name='a', value_name='b')
print (data)
          date  a     b
0   2017-09-05  A    25
1   2017-09-06  A   261
2   2017-09-07  A   188
3   2017-09-08  A   200
4   2017-09-09  A   292
5   2017-09-05  B   261
6   2017-09-06  B  1519
7   2017-09-07  B  1545
8   2017-09-08  B  2110
9   2017-09-09  B  2391
10  2017-09-05  C    31
11  2017-09-06  C   151
12  2017-09-07  C   144
13  2017-09-08  C   232
14  2017-09-09  C   325

ax=sns.barplot(x='date', y='b', hue='a', data=data)
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)

Pandas solution with DataFrame.plot.barand set_index:

使用DataFrame.plot.bar和的Pandas解决方案set_index

df.set_index('date').plot.bar()