<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>The pixels plotted by <code class="notranslate">imshow</code> aren't of constant size. Some are larger than others.</strong></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="import numpy as np
from matplotlib import pyplot as plt

matrix = np.diag(np.ones(100))

fig, ax = plt.subplots(figsize=(4, 4))
ax.imshow(matrix)
fig.savefig('imshow.png')

fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(matrix)
fig.savefig('imshow.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">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">matrix</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">diag</span>(<span class="pl-s1">np</span>.<span class="pl-en">ones</span>(<span class="pl-c1">100</span>))

<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-s1">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">4</span>, <span class="pl-c1">4</span>))
<span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">matrix</span>)
<span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'imshow.png'</span>)

<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-s1">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">10</span>, <span class="pl-c1">10</span>))
<span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">matrix</span>)
<span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'imshow.pdf'</span>)</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>
<p dir="auto">It's quite visible on the following figure (<code class="notranslate">imshow.png</code> from the above code, but also shown like that in jupyter) that some pixels are larger than others.</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/6806065/53899959-b080f400-403b-11e9-8252-de3526cc1bdd.png"><img src="https://user-images.githubusercontent.com/6806065/53899959-b080f400-403b-11e9-8252-de3526cc1bdd.png" alt="imshow" style="max-width: 100%;"></a></p>
<p dir="auto">While I assume it could be due to a low resolution image, the same happen for a larger <code class="notranslate">figsize</code>, even if the file is saved in a vector format such as PDF. Look (closely) at <a href="https://github.com/matplotlib/matplotlib/files/2937465/imshow.pdf"><code class="notranslate">imshow.pdf</code></a> from the above code.</p>
<p dir="auto"><strong>Expected outcome</strong></p>
<p dir="auto">All pixels must be of the same size.</p>
<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system: Arch Linux</li>
<li>Matplotlib version: 3.0.3</li>
<li>Matplotlib backend: <code class="notranslate">module://ipykernel.pylab.backend_inline</code></li>
<li>Python version: 3.7</li>
<li>Jupyter version (if applicable): jupyterlab 0.35.4</li>
</ul>
<p dir="auto">matplotlib was installed from pacman (the Arch Linux package manager)</p>
<h3 dir="auto">Bug report</h3>
<p dir="auto">As has been previously reported, imshow misaligns grid lines and image data. This gives a tiny reproducible example and discusses the specific issues. The example is actually mild. I have larger data sets where the effect is worse, added at the end; but the following tiny example demonstrates the core issues.</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="import matplotlib.pyplot as mp
import numpy as np
matrix = np.zeros((40, 40), dtype='int32')
np.fill_diagonal(matrix, 1)
mp.imshow(matrix)
axis = mp.gca()
axis.set_yticks([10.5, 20.5, 30.5], minor='True')
axis.set_xticks([10.5, 20.5, 30.5], minor='True')
axis.yaxis.grid(True, which='minor')
axis.xaxis.grid(True, which='minor')
mp.savefig(fname='bug.png')"><pre class="notranslate"><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">mp</span>
<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-s1">matrix</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-c1">40</span>, <span class="pl-c1">40</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'int32'</span>)
<span class="pl-s1">np</span>.<span class="pl-en">fill_diagonal</span>(<span class="pl-s1">matrix</span>, <span class="pl-c1">1</span>)
<span class="pl-s1">mp</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">matrix</span>)
<span class="pl-s1">axis</span> <span class="pl-c1">=</span> <span class="pl-s1">mp</span>.<span class="pl-en">gca</span>()
<span class="pl-s1">axis</span>.<span class="pl-en">set_yticks</span>([<span class="pl-c1">10.5</span>, <span class="pl-c1">20.5</span>, <span class="pl-c1">30.5</span>], <span class="pl-s1">minor</span><span class="pl-c1">=</span><span class="pl-s">'True'</span>)
<span class="pl-s1">axis</span>.<span class="pl-en">set_xticks</span>([<span class="pl-c1">10.5</span>, <span class="pl-c1">20.5</span>, <span class="pl-c1">30.5</span>], <span class="pl-s1">minor</span><span class="pl-c1">=</span><span class="pl-s">'True'</span>)
<span class="pl-s1">axis</span>.<span class="pl-s1">yaxis</span>.<span class="pl-en">grid</span>(<span class="pl-c1">True</span>, <span class="pl-s1">which</span><span class="pl-c1">=</span><span class="pl-s">'minor'</span>)
<span class="pl-s1">axis</span>.<span class="pl-s1">xaxis</span>.<span class="pl-en">grid</span>(<span class="pl-c1">True</span>, <span class="pl-s1">which</span><span class="pl-c1">=</span><span class="pl-s">'minor'</span>)
<span class="pl-s1">mp</span>.<span class="pl-en">savefig</span>(<span class="pl-s1">fname</span><span class="pl-c1">=</span><span class="pl-s">'bug.png'</span>)</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/68190/49508883-8ed16e80-f88c-11e8-8c9c-263157c5d729.png"><img src="https://user-images.githubusercontent.com/68190/49508883-8ed16e80-f88c-11e8-8c9c-263157c5d729.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Elaboration</strong></p>
<p dir="auto">Due to rounding, each cell of the data can have one of two sizes in pixels the image. This is understandable and inevitable. I added a red border using the smaller size to demonstrate the rounding issues, and annotated points where the actual issues arise. You would want to zoom in on the image to properly see these issues.</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/68190/49511653-68afcc80-f894-11e8-9629-874012911b5b.png"><img src="https://user-images.githubusercontent.com/68190/49511653-68afcc80-f894-11e8-9629-874012911b5b.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">Points:</p>
<ol dir="auto">
<li>
<p dir="auto">The cell at (0, 0) is clipped by 2 pixels in the Y direction and 1 pixel in the X direction. Compare to the cell at (39, 39) where the grid line is completely outside the cell.</p>
</li>
<li>
<p dir="auto">Grid lines placed exactly between cells must, of necessity, overlap one of the cells. However, for best visual results, if the grid line is between a "big" cell and a "small" cell, the result is much better-looking if the grid line overlaps the "big" cell. You could argue this is nit-picking, but if rounding issues are addressed, this should be at least in the "to be considered" issues list.</p>
</li>
<li>
<p dir="auto">The grid line which should have been between the cells is actually <strong>inside</strong> a cell. This seems to be an actual bug; on its face, there's no reason that this should ever happen, rounding errors or no rounding errors. It is worth noting that this seems to always happen in the Y direction and never in the X direction. There seems to be different treatment of X and Y axes when it comes to grid placement and rounding, expressed both at this point and at the first point. On it face, the code should be identical in both cases, so such axis-dependent artifacts should "never happen". If/when someone actually debugs the code, I would suggest that the first step would be looking for reasons why there is actually a difference between the X and Y axes.</p>
</li>
<li>
<p dir="auto">Neither the (0, 0) cell nor the (39, 39) cell are "big" (with an extra pixel), because "big" cells appear only every 2/3 cells, and the (0, 0) cell and (39, 39) cells are too close to an existing big cell. Also, none of the "big" cells are exactly 2 or 3 cells away from either (0, 0) or (39, 39). Since the decision of whether a cell is "big" depends on rounding errors, one would expect that if every 4th cell is a "big" cell, then the 4th cell (starting from (0, 0), say) would be the 1st "big" cell. Perhaps using "round" shifts the pattern by one-half the repeat rate? Something to ponder when comparing the position of grid lines and cells (perhaps one us using "round" and another is using "floor")?</p>
</li>
</ol>
<p dir="auto"><strong>Matplotlib version</strong></p>
<ul dir="auto">
<li>Operating system: Linux, CentOs 7.5</li>
<li>Matplotlib version: 2.2.3 (installed using <code class="notranslate">pip</code>).</li>
<li>Matplotlib backend: TkAgg</li>
<li>Python version: 3.6</li>
<li>Jupyter version (if applicable): NA</li>
<li>Other libraries: NA</li>
</ul>
<p dir="auto"><strong>Example with worse effects</strong></p>
<p dir="auto">The following example is from my actual program. The grid line coordinates are identical in the X and Y axis; you can see in the top-right corner the top-most gray grid line is too low (purple pixels visible above it). Even worse, the matrix has an all-0 diagonal. This ends up being <strong>several pixels</strong> away from the top-right grid corner. This seems to go way beyond a +/- one pixel rounding error to point at a more systematic problem. Perhaps the problem is that the grid Y size does not match the data's Y size in some way.</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/68190/49512472-bd544700-f896-11e8-9c8f-b64ae0dfb5bf.png"><img src="https://user-images.githubusercontent.com/68190/49512472-bd544700-f896-11e8-9c8f-b64ae0dfb5bf.png" alt="image" style="max-width: 100%;"></a></p>