Pandas annotate dataframe hist
<pre><code>df = { 'id' : pd.Series([1,1,1,1,2,2,2,2,3,3,4,4,4])}

num_id = df['id'].value_counts().hist(bins=2)
</code></pre>

<p>I an get a nice histogram with the count of number of ids that fall into each bin. </p>

<p>Question is, how do i add annotations to each bar to show the count? It could be in the middle with white text. </p>

<p>I know there is an <code>ax</code> parameter that we can specify in <code>hist()</code> but i am not sure how. </p>

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

<p>Here you are (with the <code>import</code> statements, just in case):</p>

<pre><code>import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({ 'id' : [1,1,1,1,2,2,2,2,3,3,4,4,4]})

fig, ax = plt.subplots()
freq, bins, _ = ax.hist(df['id'].value_counts(), 2, facecolor='skyblue')
ax.grid(True)
for i, n in enumerate(freq):
    ax.text(bins[i]+0.5, 2, n)
ax.set_xlabel('Bins')
ax.set_ylabel('Freq')
plt.show()
</code></pre>

<p><a href="https://i.stack.imgur.com/t85V1.png" rel="nofollow"><img src="https://i.stack.imgur.com/t85V1.png" alt="Histogram, with Text"></a></p>

python pandas matplotlib