<p dir="auto">(This is also on stack overflow <a href="https://stackoverflow.com/q/45619654/5134817" rel="nofollow">https://stackoverflow.com/q/45619654/5134817</a>)</p>
<p dir="auto">I have the following code (below) to produce a plot (a confusion matrix), and I want black lines between the squares, as below:</p>
<p dir="auto"><strong>Code</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np
import matplotlib as mpl
from matplotlib.backends.backend_pgf import FigureCanvasPgf
mpl.backend_bases.register_backend('pdf', FigureCanvasPgf)
pgf_with_rc_fonts = {'figure.figsize': (15,9.3)} # Usually much longer.
mpl.rcParams.update(pgf_with_rc_fonts)
import matplotlib.pylab as plt
cm = np.array([[0.4, 0.6], [0.9, 0.1]])
classes = ['False', 'True']
plt.clf()
cmap = cmap=plt.cm.Blues
plt.imshow(cm, interpolation='none', cmap=cmap, aspect='equal', vmax=1, vmin=0)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes)
plt.yticks(tick_marks, classes)
plt.gca().set_xticks(np.arange(-.5, len(tick_marks), 1), minor=True)
plt.gca().set_yticks(np.arange(-.5, len(tick_marks), 1), minor=True)
plt.gca().grid(which='minor', color='k', linestyle='-')
plt.gca().tick_params('both', length=0, width=1, which='minor')"><pre class="notranslate"><code class="notranslate">import numpy as np
import matplotlib as mpl
from matplotlib.backends.backend_pgf import FigureCanvasPgf
mpl.backend_bases.register_backend('pdf', FigureCanvasPgf)
pgf_with_rc_fonts = {'figure.figsize': (15,9.3)} # Usually much longer.
mpl.rcParams.update(pgf_with_rc_fonts)
import matplotlib.pylab as plt
cm = np.array([[0.4, 0.6], [0.9, 0.1]])
classes = ['False', 'True']
plt.clf()
cmap = cmap=plt.cm.Blues
plt.imshow(cm, interpolation='none', cmap=cmap, aspect='equal', vmax=1, vmin=0)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes)
plt.yticks(tick_marks, classes)
plt.gca().set_xticks(np.arange(-.5, len(tick_marks), 1), minor=True)
plt.gca().set_yticks(np.arange(-.5, len(tick_marks), 1), minor=True)
plt.gca().grid(which='minor', color='k', linestyle='-')
plt.gca().tick_params('both', length=0, width=1, which='minor')
</code></pre></div>
<p dir="auto"><strong>Plots</strong></p>
<p dir="auto">This produces the following plot:</p>
<p dir="auto"><a href="https://i.stack.imgur.com/e3cwT.png" rel="nofollow"><img src="https://camo.githubusercontent.com/786eb15eb1720c0265dd85b16dd1b820d230067ecc138f4230f1007f9616baa0/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f65336377542e706e67" alt="Original" data-canonical-src="https://i.stack.imgur.com/e3cwT.png" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>The problem region</strong></p>
<p dir="auto">And the annoying thing is that:either than the box is not of the correct height, else the y axis minor tick is too low. I am not sure which, but <strong>it gives the ugly border</strong>:</p>
<p dir="auto"><a href="https://i.stack.imgur.com/IvoFC.png" rel="nofollow"><img src="https://camo.githubusercontent.com/06e47377f8a93e25fa41dbb62a75f7d1bf9be4d930ab5d94360ed3c7c9890131/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f49766f46432e706e67" alt="Ugly" data-canonical-src="https://i.stack.imgur.com/IvoFC.png" style="max-width: 100%;"></a></p>
<p dir="auto">However, when I remove the the parameter <code class="notranslate">'figure.figsize': (15,9.3)</code> this doesn't happen. I am unsure what is going wrong, but I want the larger figure sizes for nicer plots, and so I can use larger interactive plots.</p>
<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>