<p dir="auto">I have some problems with non-accurate placing of images created by imshow. Output to PNG works fine, but the squares are rectangular and misaligned with the grid when plotting small squares to PDF (order of magnitude around 1-2 inches for the whole plot). I didn't inspect the contents of PDF, but I suspect that coordinates might be written with too low precision.</p>
<p dir="auto">Here's a small example (maybe not the minimal possible, but hopefully short enough):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np
import matplotlib.pyplot as plt

plt.gcf().set_size_inches([1,1])
a=np.random.rand(20,20)
im = plt.imshow(a, interpolation='nearest', origin='lower', extent=[-2,2,-2,2])
plt.gca().set_xticks(np.arange(-2,2,0.2))
plt.gca().set_yticks(np.arange(-2,2,0.2))
plt.gca().xaxis.set_ticklabels([])
plt.gca().yaxis.set_ticklabels([])
plt.grid(ls=':',lw=0.1,color='white')
plt.savefig('test.pdf')"><pre class="notranslate"><code class="notranslate">import numpy as np
import matplotlib.pyplot as plt

plt.gcf().set_size_inches([1,1])
a=np.random.rand(20,20)
im = plt.imshow(a, interpolation='nearest', origin='lower', extent=[-2,2,-2,2])
plt.gca().set_xticks(np.arange(-2,2,0.2))
plt.gca().set_yticks(np.arange(-2,2,0.2))
plt.gca().xaxis.set_ticklabels([])
plt.gca().yaxis.set_ticklabels([])
plt.grid(ls=':',lw=0.1,color='white')
plt.savefig('test.pdf')
</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/15606/2550376/6e526552-b682-11e3-820b-4c39a9ef6b1b.png"><img src="https://cloud.githubusercontent.com/assets/15606/2550376/6e526552-b682-11e3-820b-4c39a9ef6b1b.png" alt="x" style="max-width: 100%;"></a></p>
<p dir="auto">I'm using python 3.3 and matplotlib 1.3.1.</p>
<p dir="auto">The following script show a mis-alignment between grid lines and image patterns while they should be aligned:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

n = 16
fig = plt.figure(figsize=(6,6))
Z = np.zeros((n,n))
Z[::2,::2] = Z[1::2,1::2] = 1
plt.imshow(Z, interpolation='none', cmap=plt.cm.gray, extent=[0,n,0,n], alpha=0.25)
plt.xticks(np.arange(0,n), []), plt.yticks(np.arange(0,n), [])
plt.grid(ls='solid')
delta = 0.01
plt.xlim(1-delta,1+delta), plt.ylim(1-delta,1+delta)
plt.savefig('pylab-grid.png')
plt.savefig('pylab-grid.pdf')"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>
<span class="pl-s1">matplotlib</span>.<span class="pl-en">use</span>(<span class="pl-s">'Agg'</span>)
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>

<span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-c1">16</span>
<span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">figure</span>(<span class="pl-s1">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">6</span>,<span class="pl-c1">6</span>))
<span class="pl-v">Z</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-s1">n</span>,<span class="pl-s1">n</span>))
<span class="pl-v">Z</span>[::<span class="pl-c1">2</span>,::<span class="pl-c1">2</span>] <span class="pl-c1">=</span> <span class="pl-v">Z</span>[<span class="pl-c1">1</span>::<span class="pl-c1">2</span>,<span class="pl-c1">1</span>::<span class="pl-c1">2</span>] <span class="pl-c1">=</span> <span class="pl-c1">1</span>
<span class="pl-s1">plt</span>.<span class="pl-en">imshow</span>(<span class="pl-v">Z</span>, <span class="pl-s1">interpolation</span><span class="pl-c1">=</span><span class="pl-s">'none'</span>, <span class="pl-s1">cmap</span><span class="pl-c1">=</span><span class="pl-s1">plt</span>.<span class="pl-s1">cm</span>.<span class="pl-s1">gray</span>, <span class="pl-s1">extent</span><span class="pl-c1">=</span>[<span class="pl-c1">0</span>,<span class="pl-s1">n</span>,<span class="pl-c1">0</span>,<span class="pl-s1">n</span>], <span class="pl-s1">alpha</span><span class="pl-c1">=</span><span class="pl-c1">0.25</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">xticks</span>(<span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">0</span>,<span class="pl-s1">n</span>), []), <span class="pl-s1">plt</span>.<span class="pl-en">yticks</span>(<span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">0</span>,<span class="pl-s1">n</span>), [])
<span class="pl-s1">plt</span>.<span class="pl-en">grid</span>(<span class="pl-s1">ls</span><span class="pl-c1">=</span><span class="pl-s">'solid'</span>)
<span class="pl-s1">delta</span> <span class="pl-c1">=</span> <span class="pl-c1">0.01</span>
<span class="pl-s1">plt</span>.<span class="pl-en">xlim</span>(<span class="pl-c1">1</span><span class="pl-c1">-</span><span class="pl-s1">delta</span>,<span class="pl-c1">1</span><span class="pl-c1">+</span><span class="pl-s1">delta</span>), <span class="pl-s1">plt</span>.<span class="pl-en">ylim</span>(<span class="pl-c1">1</span><span class="pl-c1">-</span><span class="pl-s1">delta</span>,<span class="pl-c1">1</span><span class="pl-c1">+</span><span class="pl-s1">delta</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'pylab-grid.png'</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'pylab-grid.pdf'</span>)</pre></div>