msklc
msklc

Reputation: 604

How can I add the counts to the histogram plot?

I want to add the counts data of histogram to the plot in matplotlib. Here is my data;

import matplotlib.pyplot as plt
data = [['B', 1], ['C', 2], ['A', 3],['A', 4], ['B', 5], ['B', 6],['A', 7], ['B', 8], ['C', 9],['D',10]] 
df = pd.DataFrame(data, columns = ['Name', 'Count']) 
plt.hist(df['Name'])
plt.show()

The result is like this; result1

I tried to use plt.text and value_counts() but their sorting are different...

import matplotlib.pyplot as plt
data = [['B', 1], ['C', 2], ['A', 3],['A', 4], ['B', 5], ['B', 6],['A', 7], ['B', 8], ['C', 9],['D',10]] 
df = pd.DataFrame(data, columns = ['Name', 'Count']) 

xvals = np.arange(len(df['Name'].value_counts()))
yvals = list(df['Name'].value_counts())
for i in range(len(df['Name'].value_counts())):
    plt.text(x=xvals[i], y=yvals[i],s=df['Name'].value_counts(sort=False)[i])

plt.hist(df['Name'])
plt.show()

So, I get a result like this; result2

I think it mustn't be so difficult but I can't find any solution.

Upvotes: 2

Views: 11781

Answers (2)

pyplot.hist returns the lengths and locations of the bins. You can make use of this by saving the returns from pyplot.hist:

import matplotlib.pyplot as plt
import pandas as pd 
import numpy as np

data = [['B', 1], ['C', 2], ['A', 3],['A', 4], ['B', 5], ['B', 6],['A', 7], ['B', 8], ['C', 9],['D',10]] 
df = pd.DataFrame(data, columns = ['Name', 'Count']) 

xvals = np.arange(len(df['Name'].value_counts()))
yvals = list(df['Name'].value_counts())

counts, bins, _ = plt.hist(df['Name'])

for n, b in zip(counts, bins):
        plt.gca().text(b + 0.1, n, str(n))  # +0.1 to center text

plt.show()

This results in: enter image description here

If you want to remove the 0.0s of the empty bars, change the for loop to this:

for n, b in zip(counts, bins):
    if n > 0:
        plt.gca().text(b + 0.1, n, str(n))  # +0.1 to center text

Result: enter image description here

Upvotes: 2

abhilb
abhilb

Reputation: 5757

You can try something like this:

hist returns counts, bins and patches. patches is a list of rectangles. Then you can annotate the axis using the count and coordinates of the patch rectangles.

import numpy as np
import matplotlib.pyplot as plt

# generate some random data
x = np.random.randn(10000)
x = x * 100
x = x.astype(np.int)

# plot the histogram of the data
bins = np.arange(-300,300,20)
fig = plt.figure(figsize=(15,4))
ax = plt.gca()
counts, _, patches = ax.hist(x, bins=bins,edgecolor='r')
for count, patch in zip(counts,patches):
    ax.annotate(str(int(count)), xy=(patch.get_x(), patch.get_height()))
plt.show()

Upvotes: 5

Related Questions