Georg Heiler Georg Heiler - 2 months ago 25
Python Question

Python stacked histogram grouped data

How can I create a stacked Histogram like this:
enter image description here

import numpy as np
import pylab as P
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')

%pylab inline
pylab.rcParams['figure.figsize'] = (15, 4)
mu, sigma = 200, 25
x = mu + sigma*P.randn(1000,2)
n, bins, patches = P.hist(x, 10, normed=1, histtype='bar', stacked=True)
P.show()


For grouped data.

import pandas as pd
df1 = pd.DataFrame({'A': [1,2,3,4,5,6,7,8,9],'group': [1,0,0,0,0,1,1,1,1]})


As
df1.A.hist(by=df1.group, bins='doane', stacked=True)
or

df1.A.hist(by=df1.group, bins='doane')


only result in:
enter image description here

Answer

You can pivot your DataFrame so that each group is in a different column and then generate the histogram.

df1 = pd.DataFrame({'A': [1,2,3,4,5,6,7,8,9],'group': [1,0,0,0,0,1,1,1,1]})
df1.pivot(columns='group', values='A').plot.hist(stacked=True)

enter image description here

Edit: Added the stacked=True parameter to hist()

Comments