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.
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
Improve the answer, the best way could be:
titanic.pivot(columns='Survived').Age.plot(kind = 'hist', stacked=True)
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)
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With