<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto"><code class="notranslate">imshow</code> (and friends, e.g., <code class="notranslate">pcolormesh</code>) don't tightly fit the image inside the axes.  I suspect an off-by-one error somewhere (see the hint section at the bottom).</p>
<p dir="auto"><strong>Code for reproduction</strong></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from matplotlib import pyplot as plt

fig = plt.figure(figsize=(3.423,2.92917))
ax = plt.Axes(fig, [0.2,0.2,0.2,0.2])
fig.add_axes(ax)

ax.imshow([[1,2],[3,4]],rasterized=True)
plt.savefig('gap.pdf')"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">import</span> <span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</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">3.423</span>,<span class="pl-c1">2.92917</span>))
<span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-v">Axes</span>(<span class="pl-s1">fig</span>, [<span class="pl-c1">0.2</span>,<span class="pl-c1">0.2</span>,<span class="pl-c1">0.2</span>,<span class="pl-c1">0.2</span>])
<span class="pl-s1">fig</span>.<span class="pl-en">add_axes</span>(<span class="pl-s1">ax</span>)

<span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>([[<span class="pl-c1">1</span>,<span class="pl-c1">2</span>],[<span class="pl-c1">3</span>,<span class="pl-c1">4</span>]],<span class="pl-s1">rasterized</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
<span class="pl-s1">plt</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'gap.pdf'</span>)</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>
<p dir="auto"><a href="https://github.com/matplotlib/matplotlib/files/4973079/gap.pdf">gap.pdf</a></p>
<p dir="auto"><strong>Expected outcome</strong></p>
<p dir="auto">There should be no gap between the x axis, and the image.</p>
<p dir="auto"><strong>Matplotlib version</strong></p>
<ul dir="auto">
<li>Operating system: linux</li>
<li>Matplotlib version: 3.2.2 (but also later 3.4.0)</li>
<li>Python version: 3.8</li>
</ul>
<h1 dir="auto">Hints</h1>
<p dir="auto">I don't have time to dig into this right now, but it is corrected (for my particular figure size) by subtracting 1 from the <code class="notranslate">y</code> parameter in the <code class="notranslate">draw_image</code> call in the <code class="notranslate">stop_rasterizing</code> definition of <code class="notranslate">backend_mixed.py</code>. I.e., change</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="self._renderer.draw_image(
    gc,
    l * self._figdpi / self.dpi,
    (height-b-h) * self._figdpi / self.dpi,
    image)"><pre class="notranslate"><span class="pl-s1">self</span>.<span class="pl-s1">_renderer</span>.<span class="pl-en">draw_image</span>(
    <span class="pl-s1">gc</span>,
    <span class="pl-s1">l</span> <span class="pl-c1">*</span> <span class="pl-s1">self</span>.<span class="pl-s1">_figdpi</span> <span class="pl-c1">/</span> <span class="pl-s1">self</span>.<span class="pl-s1">dpi</span>,
    (<span class="pl-s1">height</span><span class="pl-c1">-</span><span class="pl-s1">b</span><span class="pl-c1">-</span><span class="pl-s1">h</span>) <span class="pl-c1">*</span> <span class="pl-s1">self</span>.<span class="pl-s1">_figdpi</span> <span class="pl-c1">/</span> <span class="pl-s1">self</span>.<span class="pl-s1">dpi</span>,
    <span class="pl-s1">image</span>)</pre></div>
<p dir="auto">to</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="self._renderer.draw_image(
    gc,
    l * self._figdpi / self.dpi,
    (height-b-h) * self._figdpi / self.dpi - 1,
    image)"><pre class="notranslate"><span class="pl-s1">self</span>.<span class="pl-s1">_renderer</span>.<span class="pl-en">draw_image</span>(
    <span class="pl-s1">gc</span>,
    <span class="pl-s1">l</span> <span class="pl-c1">*</span> <span class="pl-s1">self</span>.<span class="pl-s1">_figdpi</span> <span class="pl-c1">/</span> <span class="pl-s1">self</span>.<span class="pl-s1">dpi</span>,
    (<span class="pl-s1">height</span><span class="pl-c1">-</span><span class="pl-s1">b</span><span class="pl-c1">-</span><span class="pl-s1">h</span>) <span class="pl-c1">*</span> <span class="pl-s1">self</span>.<span class="pl-s1">_figdpi</span> <span class="pl-c1">/</span> <span class="pl-s1">self</span>.<span class="pl-s1">dpi</span> <span class="pl-c1">-</span> <span class="pl-c1">1</span>,
    <span class="pl-s1">image</span>)</pre></div>
<p dir="auto">Hopefully that might help someone track this down.</p>
<p dir="auto">The following example (with Matplotlib 2.0.0b3) causes the colorbar inside the colorbar axes to be offset when outputting to PDF with a low DPI:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
im = ax.imshow([[1]])
fig.colorbar(im)
fig.savefig('colorbar_test.pdf', dpi=7)"><pre class="notranslate"><code class="notranslate">import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
im = ax.imshow([[1]])
fig.colorbar(im)
fig.savefig('colorbar_test.pdf', dpi=7)
</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/314716/17089570/05e193be-51f6-11e6-953a-408df28dd8e4.png"><img src="https://cloud.githubusercontent.com/assets/314716/17089570/05e193be-51f6-11e6-953a-408df28dd8e4.png" alt="colorbar_test" style="max-width: 100%;"></a></p>
<p dir="auto">This wasn't an issue with Matplotlib 1.4:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/314716/17089590/4598aa88-51f6-11e6-8186-9829c4118c95.png"><img src="https://cloud.githubusercontent.com/assets/314716/17089590/4598aa88-51f6-11e6-8186-9829c4118c95.png" alt="colorbar_test" style="max-width: 100%;"></a></p>
<p dir="auto">and the bug was apparently introduces in Matplotlib 1.5:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/314716/17089597/668bb4d8-51f6-11e6-8261-28e7becd97d6.png"><img src="https://cloud.githubusercontent.com/assets/314716/17089597/668bb4d8-51f6-11e6-8261-28e7becd97d6.png" alt="colorbar_test" style="max-width: 100%;"></a></p>