Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seaborn multiple variables group bar plot

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)

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

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

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)

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

like image 950
jayko03 Avatar asked Mar 27 '18 06:03

jayko03


1 Answers

I think need melt if want use 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.bar and set_index:

df.set_index('date').plot.bar()
like image 183
jezrael Avatar answered Sep 24 '22 11:09

jezrael