Makaroni Makaroni - 7 months ago 21
Python Question

Pandas plot on 3 variables

Can someone help me define best way of visualizing 3 columns in pandas? I tried using stacked bar plot and searched for other solutions on SO, but nothing worked. Any help is appreciated. Here is a dummy pandas dataframe:

Name hour var
Nem 0 2
Kiz 4 1
Hue 5 2
Kiz 0 3
Nem 7 7

Answer

UPDATE: is that what you want?

(df.pivot_table(index='Name', columns='hour', values='var',
                aggfunc='sum', fill_value=0)
   .plot.bar(stacked=True)
)

enter image description here

Explanation:

In [55]: (df.pivot_table(index='Name', columns='hour', values='var',
   ....:                 aggfunc='sum', fill_value=0)
   ....: )
Out[55]:
hour  0  3  4  5  7
Name
Hue   0  6  0  2  0
Kiz   3  0  1  0  0
Nem   2  5  0  0  7

OLD answer:

you can use seaborn module for that:

import seaborn as sns

sns.barplot(x='Name', y='var', hue='hour', data=df, saturation=0.8)

enter image description here

data:

In [20]: df
Out[20]:
  Name  hour  var
0  Nem     0    2
1  Nem     3    5
2  Kiz     4    1
3  Hue     5    2
4  Kiz     0    3
5  Nem     7    7
6  Hue     3    6
Comments