I was trying to draw a histogram of a sine curve as below
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
dt=0.01 #delta t
T_upper=100. #time upper limit
t=np.arange(0.,T_upper+dt,dt) #time
#print t[-1]
a=2. #wave amplitude
T=2. #wave period
phi=np.pi/2 #phase
eta=a*np.sin(2*np.pi/T*t+phi)
#plt.plot(t,eta)
plt.hist(eta,normed=True,bins=20,color='blue',edgecolor='black',linewidth=1)
plt.ylabel('Probability')
plt.xlim(eta.min()*1.25,eta.max()*1.25)
plt.xticks([-2,-1,0,1,2])
plt.show()
And below is what I get enter image description here
It can be seen that bins do not align with their values. What I want is to have bins locate at the centre of their values, for example at min, -2 and max 2.
I tried using bins=np.arange(2)-0.5, it does not work for me, I think it is because of the interval of this histogram is not integers, but I am not sure how to fix it, can anyone help, thanks in advance.