Seaborn多变量组条形图 [英] seaborn multiple variables group bar plot

查看:351
本文介绍了Seaborn多变量组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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.

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

但是,我想在其中显示Seaborn或其他图书馆以提高我的技能。
我找到了非常接近的答案(如何绘制带有两个类别和四个系列的条形图?)。

在建议的答案中,它的代码是

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

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

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

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

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

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

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

推荐答案

我认为需要 融化 (如果要使用 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)

具有 DataFrame.plot.bar set_index

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

这篇关于Seaborn多变量组条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆