Creating a matplotlib or seaborn histogram which uses percent rather than count?
<p>Specifically I'm dealing with the Kaggle Titanic dataset. I've plotted a stacked histogram which shows ages that survived and died upon the titanic. Code below.</p>

<pre><code>figure = plt.figure(figsize=(15,8))
plt.hist([data[data['Survived']==1]['Age'], data[data['Survived']==0]['Age']], stacked=True, bins=30, label=['Survived','Dead'])
plt.xlabel('Age')
plt.ylabel('Number of passengers')
plt.legend()
</code></pre>

<p>I would like to alter the chart to show a single chart per bin of the percentage in that age group that survived. E.g. if a bin contained the ages between 10-20 years of age and 60% of people aboard the titanic in that age group survived, then the height would line up 60% along the y-axis.</p>

<p>Edit: I may have given a poor explanation to what I'm looking for. Rather than alter the y-axis values, I'm looking to change the actual shape of the bars based on the percentage that survived.</p>

<p>The first bin on the graph shows roughly 65% survived in that age group. I would like this bin to line up against the y-axis at 65%. The following bins look to be 90%, 50%, 10% respectively, and so on.</p>

<p><a href="https://i.stack.imgur.com/5OQiQ.png" rel="nofollow"><img src="https://i.stack.imgur.com/5OQiQ.png" alt=""></a></p>

<p>The graph would end up actually looking something like this:</p>

<p><a href="https://i.stack.imgur.com/qVQI3.png" rel="nofollow"><img src="https://i.stack.imgur.com/qVQI3.png" alt="enter image description here"></a></p>

<p><code>pd.Series.hist</code> uses <code>np.histogram</code> underneath.</p>

<p>Let's explore that</p>

<pre><code>np.random.seed([3,1415])
s = pd.Series(np.random.randn(100))
d = np.histogram(s, normed=True)
print('\nthese are the normalized counts\n')
print(d[0])
print('\nthese are the bin values, or average of the bin edges\n')
print(d[1])

these are the normalized counts

[ 0.11552497  0.18483996  0.06931498  0.32346993  0.39278491  0.36967992
  0.32346993  0.25415494  0.25415494  0.02310499]

these are the bin edges

[-2.25905503 -1.82624818 -1.39344133 -0.96063448 -0.52782764 -0.09502079
  0.33778606  0.77059291  1.20339976  1.6362066   2.06901345]
</code></pre>

<p>We can plot these while calculating the mean bin edges</p>

<pre><code>pd.Series(d[0], pd.Series(d[1]).rolling(2).mean().dropna().round(2).values).plot.bar()
</code></pre>

<p><a href="https://i.stack.imgur.com/fZxSi.png" rel="nofollow"><img src="https://i.stack.imgur.com/fZxSi.png" alt="enter image description here"></a></p>

<p><strong><em>ACTUAL ANSWER</em></strong><br>
OR</p>

<p>We could have simply passed <code>normed=True</code> to the <code>pd.Series.hist</code> method.  Which passes it along to <code>np.histogram</code></p>

<pre><code>s.hist(normed=True)
</code></pre>

<p><a href="https://i.stack.imgur.com/aypHc.png" rel="nofollow"><img src="https://i.stack.imgur.com/aypHc.png" alt="enter image description here"></a></p>

python pandas matplotlib dataset histogram