Why is my grid offset in this example?
<p>I am trying to draw a randomly occupied grid with matplotlib. The grid looks offset from the blocks by a random amount:</p>

<p><a href="https://i.stack.imgur.com/zI5bl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zI5bl.png" alt="The grid"></a></p>

<p>Here is the code:</p>

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

# Make a 10x10 grid...
nrows, ncols = 10,10
# Fill the cells randomly with 0s and 1s
image = np.random.randint(2, size = (nrows, ncols))

# Make grid
vgrid = []
for i in range(nrows + 1):
    vgrid.append((i - 0.5, i - 0.5))
    vgrid.append((- 0.5, 9.5))

hgrid = []
for i in range(ncols + 1):
    hgrid.append((- 0.5, 9.5))
    hgrid.append((i - 0.5, i - 0.5))

row_labels = range(nrows)
col_labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'j']

plt.matshow(image, cmap='Greys')
for i in range(11):
    plt.plot(hgrid[2 * i], hgrid[2 * i + 1], 'k-')
    plt.plot(vgrid[2 * i], vgrid[2 * i + 1], 'k-')

plt.axis([-0.5, 9.5, -0.5, 9.5])
plt.xticks(range(ncols), col_labels)
plt.yticks(range(nrows), row_labels)

plt.show()
</code></pre>

<p>The problem seems to happen when I enforce a plot area; this line:</p>

<pre><code>plt.axis([-0.5, 9.5, -0.5, 9.5])
</code></pre>

<p>Also, please feel free to suggest a better method. I am new to pyplot.</p>