Assuming you have some numpy array a
full of integers then the code below will produce the bar chart you desire.
It uses np.bincount
to count the number of values, note that it only works for non-negative integers.
Also note that I have adjusted the indices so that the plot centrally rather than to the left (using ind-width/2.
).
import matplotlib.pyplot as plt
import numpy as np
# Generate some random data.
N=300
a = np.random.random_integers(low=0, high=20, size=N)
# Use bincount and nonzero to generate your data in the correct format.
b = np.bincount(a)
ind = np.nonzero(b)[0]
width=0.8
fig, ax = plt.subplots()
ax.bar(ind-width/2., b)
plt.show()