16
votes

i am trying to create a stacked histogram of grouped values using this code:

titanic.groupby('Survived').Age.hist(stacked=True)

But I am getting this histogram without stacked bars.

enter image description here

How can i get the histogram's bar stacked without having to use matplotlib directly or iterating over groups?

Dataset used: https://www.udacity.com/api/nodes/5454512672/supplemental_media/titanic-datacsv/download

4

4 Answers

20
votes

Improve the answer, the best way could be:

titanic.pivot(columns='Survived').Age.plot(kind = 'hist', stacked=True)

enter image description here

12
votes

The best way that I found so far is to create a new dataframe with the groups:

pd.DataFrame({'Non-Survivors': titanic.groupby('Survived').get_group(0).Age,
              'Survivors':   titanic.groupby('Survived').get_group(1).Age})
            .plot.hist(stacked=True)

enter image description here

4
votes

This solution uses a bar plot instead of a histogram but I think it gives you what you are looking for.

titanic.groupby(['Survived', pd.cut(titanic['Age'], np.arange(0,100,10))])\
       .size()\
       .unstack(0)\
       .plot.bar(stacked=True)

enter image description here

4
votes

I defined a custom function that leverages np.histogram
Also note that the histogram groups are calculated within groups of 'Survived'

def hist(x):
    h, e = np.histogram(x.dropna(), range=(0, 80))
    e = e.astype(int)
    return pd.Series(h, zip(e[:-1], e[1:]))

kw = dict(stacked=True, width=1, rot=45)
titanic.groupby('Survived').Age.apply(hist).unstack(0).plot.bar(**kw)

enter image description here