Not solution solely using plt.hist()
but with some added functionality.
If you don't want to specify your bins beforehand and only plot densities bars, but also want to display the bin counts you can use the following.
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(100)
density, bins, _ = plt.hist(data, density=True, bins=20)
count, _ = np.histogram(data, bins)
for x,y,num in zip(bins, density, count):
if num != 0:
plt.text(x, y+0.05, num, fontsize=10, rotation=-90) # x,y,str
The result looks as follows:
