Python histogram is located on the right side of exact solution
<p>I am working on a project in thermal physics, and for that I need to compare a histogram with a smooth curve. My problem is that the histogram is placed to the right for the curve (the curve is consistent going though the upper left corner of the bars):</p>

<p><img src="http://i.stack.imgur.com/4wHGV.png" alt="Figure 1"></p>

<p>I want the curve to go though the middle of the top of the bars, like it should. It might looks like a trifle, but it really irritates me. I hope someone can help me! </p>

<p>The program looks like this:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

#--Constants--
M = 20      # Jmax
N = 1000    # Number of J values
T = 50      # Actually T/theta_r

J1 = np.linspace(0,M,N)
J2 = np.linspace(0,M,M+1)

#--Calculate z--
def z(J):
    return(2*J+1)*np.exp(-J*(J+1)/T)

#--Plot--
width = .9                           #Width of columns
plt.bar(J2, z(J2), width=width)      #Plotting histogram
#plt.xticks(ind + width / 2, ind)     #Replacing the indexes under the columns
plt.plot(J1,z(J1),'-r', linewidth=2)
SZ={'size':'16'}
plt.title('Different terms $z(j)$ plotted as function of $j$',**SZ)
plt.xlabel('$j$',**SZ)
plt.ylabel('$z(j)$',**SZ)
plt.show()
</code></pre>

<p>The first argument to <a href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar" rel="nofollow"><code>plt.bar</code></a> specifies the positions of the <em>left-hand edges</em> for each bar. To make the <em>centre</em> of each bar to align with your plot of <code>z(J2)</code> you need to offset the positions of the edges by minus half the bar width:</p>

<pre><code>plt.bar(J2 - 0.5 * width, z(J2), width=width)
</code></pre>

python matplotlib histogram