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.