<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/83c710b6f795a5c7f7c7c1e6765212a93e655a010dcb5a60bee5ed3c321af020/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3533303938382f323231363537332f31613966393434382d396130642d313165332d383238382d6434633337646265666339322e706e67"><img src="https://camo.githubusercontent.com/83c710b6f795a5c7f7c7c1e6765212a93e655a010dcb5a60bee5ed3c321af020/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3533303938382f323231363537332f31613966393434382d396130642d313165332d383238382d6434633337646265666339322e706e67" alt="test image" data-canonical-src="https://f.cloud.github.com/assets/530988/2216573/1a9f9448-9a0d-11e3-8288-d4c37dbefc92.png" style="max-width: 100%;"></a></p>
<p dir="auto">I believe the spacing between the polygons shouldn't be there. Below is my code. Is this a bug or am I missing some option? It happens on all backends.</p>
<p dir="auto">I know that I can enlarge the polygons slightly so that they overlap each other which then removes these spaces, but this approach is only suitable for very simple polygons like the rectangular ones above, and in my real use-case I have arbitrarily shaped polygons, so that's not really an option.</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 as mpl
mpl.use('agg')
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection

def createPolygons(xv, yv):
    assert xv.shape == yv.shape
    xy = np.dstack((xv,yv))

    # adapted from matplotlib.collections.QuadMesh.convert_mesh_to_paths
    verts = np.concatenate((
                xy[0:-1, 0:-1],
                xy[0:-1, 1:  ],
                xy[1:  , 1:  ],
                xy[1:  , 0:-1],
                ), axis=2)
    verts = verts.reshape((xv.shape[0]-1) * (xv.shape[1]-1), 4, 2)

    return verts

def drawPlot(verts, outputFile, blackbg, widthPx):
    if blackbg:
        mpl.rcParams['savefig.facecolor'] = 'black'
    else:
        mpl.rcParams['savefig.facecolor'] = 'white'

    ax = plt.gca()
    fig = plt.gcf()

    if blackbg:
        fig.patch.set_facecolor('black')
        ax.patch.set_facecolor('black')
        ax.spines['bottom'].set_color('white')
        ax.spines['top'].set_color('white')
        ax.spines['left'].set_color('white')
        ax.spines['right'].set_color('white')
        for i in ax.get_xticklabels(): i.set_color(&quot;white&quot;)
        for i in ax.get_yticklabels(): i.set_color(&quot;white&quot;)
        for t in ax.xaxis.get_ticklines(): t.set_color('white')
        for t in ax.yaxis.get_ticklines(): t.set_color('white')
        ax.xaxis.label.set_color('white')
        ax.yaxis.label.set_color('white')

    coll = PolyCollection(verts, facecolors=None, edgecolors='none')

    ax.add_collection(coll)
    ax.autoscale()

    width = fig.get_figwidth()
    dpi = widthPx / width

    fig.savefig(outputFile, dpi=dpi)
    plt.close(fig)


x = np.linspace(0, 10, num=10)
xv, yv = np.meshgrid(x, x)
verts = createPolygons(xv, yv)
drawPlot(verts, 'test.png', blackbg=True, widthPx=500)"><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-k">as</span> <span class="pl-s1">mpl</span>
<span class="pl-s1">mpl</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-k">from</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">collections</span> <span class="pl-k">import</span> <span class="pl-v">PolyCollection</span>

<span class="pl-k">def</span> <span class="pl-en">createPolygons</span>(<span class="pl-s1">xv</span>, <span class="pl-s1">yv</span>):
    <span class="pl-k">assert</span> <span class="pl-s1">xv</span>.<span class="pl-s1">shape</span> <span class="pl-c1">==</span> <span class="pl-s1">yv</span>.<span class="pl-s1">shape</span>
    <span class="pl-s1">xy</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">dstack</span>((<span class="pl-s1">xv</span>,<span class="pl-s1">yv</span>))

    <span class="pl-c"># adapted from matplotlib.collections.QuadMesh.convert_mesh_to_paths</span>
    <span class="pl-s1">verts</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">concatenate</span>((
                <span class="pl-s1">xy</span>[<span class="pl-c1">0</span>:<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">0</span>:<span class="pl-c1">-</span><span class="pl-c1">1</span>],
                <span class="pl-s1">xy</span>[<span class="pl-c1">0</span>:<span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-c1">1</span>:  ],
                <span class="pl-s1">xy</span>[<span class="pl-c1">1</span>:  , <span class="pl-c1">1</span>:  ],
                <span class="pl-s1">xy</span>[<span class="pl-c1">1</span>:  , <span class="pl-c1">0</span>:<span class="pl-c1">-</span><span class="pl-c1">1</span>],
                ), <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">2</span>)
    <span class="pl-s1">verts</span> <span class="pl-c1">=</span> <span class="pl-s1">verts</span>.<span class="pl-en">reshape</span>((<span class="pl-s1">xv</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">0</span>]<span class="pl-c1">-</span><span class="pl-c1">1</span>) <span class="pl-c1">*</span> (<span class="pl-s1">xv</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">1</span>]<span class="pl-c1">-</span><span class="pl-c1">1</span>), <span class="pl-c1">4</span>, <span class="pl-c1">2</span>)

    <span class="pl-k">return</span> <span class="pl-s1">verts</span>

<span class="pl-k">def</span> <span class="pl-en">drawPlot</span>(<span class="pl-s1">verts</span>, <span class="pl-s1">outputFile</span>, <span class="pl-s1">blackbg</span>, <span class="pl-s1">widthPx</span>):
    <span class="pl-k">if</span> <span class="pl-s1">blackbg</span>:
        <span class="pl-s1">mpl</span>.<span class="pl-s1">rcParams</span>[<span class="pl-s">'savefig.facecolor'</span>] <span class="pl-c1">=</span> <span class="pl-s">'black'</span>
    <span class="pl-k">else</span>:
        <span class="pl-s1">mpl</span>.<span class="pl-s1">rcParams</span>[<span class="pl-s">'savefig.facecolor'</span>] <span class="pl-c1">=</span> <span class="pl-s">'white'</span>

    <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">gca</span>()
    <span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">gcf</span>()

    <span class="pl-k">if</span> <span class="pl-s1">blackbg</span>:
        <span class="pl-s1">fig</span>.<span class="pl-s1">patch</span>.<span class="pl-en">set_facecolor</span>(<span class="pl-s">'black'</span>)
        <span class="pl-s1">ax</span>.<span class="pl-s1">patch</span>.<span class="pl-en">set_facecolor</span>(<span class="pl-s">'black'</span>)
        <span class="pl-s1">ax</span>.<span class="pl-s1">spines</span>[<span class="pl-s">'bottom'</span>].<span class="pl-en">set_color</span>(<span class="pl-s">'white'</span>)
        <span class="pl-s1">ax</span>.<span class="pl-s1">spines</span>[<span class="pl-s">'top'</span>].<span class="pl-en">set_color</span>(<span class="pl-s">'white'</span>)
        <span class="pl-s1">ax</span>.<span class="pl-s1">spines</span>[<span class="pl-s">'left'</span>].<span class="pl-en">set_color</span>(<span class="pl-s">'white'</span>)
        <span class="pl-s1">ax</span>.<span class="pl-s1">spines</span>[<span class="pl-s">'right'</span>].<span class="pl-en">set_color</span>(<span class="pl-s">'white'</span>)
        <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-s1">ax</span>.<span class="pl-en">get_xticklabels</span>(): <span class="pl-s1">i</span>.<span class="pl-en">set_color</span>(<span class="pl-s">"white"</span>)
        <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-s1">ax</span>.<span class="pl-en">get_yticklabels</span>(): <span class="pl-s1">i</span>.<span class="pl-en">set_color</span>(<span class="pl-s">"white"</span>)
        <span class="pl-k">for</span> <span class="pl-s1">t</span> <span class="pl-c1">in</span> <span class="pl-s1">ax</span>.<span class="pl-s1">xaxis</span>.<span class="pl-en">get_ticklines</span>(): <span class="pl-s1">t</span>.<span class="pl-en">set_color</span>(<span class="pl-s">'white'</span>)
        <span class="pl-k">for</span> <span class="pl-s1">t</span> <span class="pl-c1">in</span> <span class="pl-s1">ax</span>.<span class="pl-s1">yaxis</span>.<span class="pl-en">get_ticklines</span>(): <span class="pl-s1">t</span>.<span class="pl-en">set_color</span>(<span class="pl-s">'white'</span>)
        <span class="pl-s1">ax</span>.<span class="pl-s1">xaxis</span>.<span class="pl-s1">label</span>.<span class="pl-en">set_color</span>(<span class="pl-s">'white'</span>)
        <span class="pl-s1">ax</span>.<span class="pl-s1">yaxis</span>.<span class="pl-s1">label</span>.<span class="pl-en">set_color</span>(<span class="pl-s">'white'</span>)

    <span class="pl-s1">coll</span> <span class="pl-c1">=</span> <span class="pl-v">PolyCollection</span>(<span class="pl-s1">verts</span>, <span class="pl-s1">facecolors</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-s1">edgecolors</span><span class="pl-c1">=</span><span class="pl-s">'none'</span>)

    <span class="pl-s1">ax</span>.<span class="pl-en">add_collection</span>(<span class="pl-s1">coll</span>)
    <span class="pl-s1">ax</span>.<span class="pl-en">autoscale</span>()

    <span class="pl-s1">width</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">get_figwidth</span>()
    <span class="pl-s1">dpi</span> <span class="pl-c1">=</span> <span class="pl-s1">widthPx</span> <span class="pl-c1">/</span> <span class="pl-s1">width</span>

    <span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s1">outputFile</span>, <span class="pl-s1">dpi</span><span class="pl-c1">=</span><span class="pl-s1">dpi</span>)
    <span class="pl-s1">plt</span>.<span class="pl-en">close</span>(<span class="pl-s1">fig</span>)


<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">0</span>, <span class="pl-c1">10</span>, <span class="pl-s1">num</span><span class="pl-c1">=</span><span class="pl-c1">10</span>)
<span class="pl-s1">xv</span>, <span class="pl-s1">yv</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">meshgrid</span>(<span class="pl-s1">x</span>, <span class="pl-s1">x</span>)
<span class="pl-s1">verts</span> <span class="pl-c1">=</span> <span class="pl-en">createPolygons</span>(<span class="pl-s1">xv</span>, <span class="pl-s1">yv</span>)
<span class="pl-en">drawPlot</span>(<span class="pl-s1">verts</span>, <span class="pl-s">'test.png'</span>, <span class="pl-s1">blackbg</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">widthPx</span><span class="pl-c1">=</span><span class="pl-c1">500</span>)</pre></div>
<p dir="auto">This is the underlying problem raised in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="6572843" data-permission-text="Title is private" data-url="https://github.com/matplotlib/matplotlib/issues/1178" data-hovercard-type="pull_request" data-hovercard-url="/matplotlib/matplotlib/pull/1178/hovercard" href="https://github.com/matplotlib/matplotlib/pull/1178">#1178</a>.<br>
It is illustrated by the test below; note that boundary anomalies are visible in all forms--agg on the screen, and pdf and svg displayed with a viewer--but in different places depending on the viewer and the size of the figure as rendered.<br>
Note that the colorbar is rendered using pcolormesh, which has its own renderer with agg but otherwise is handled by draw_path_collection.</p>
<pre class="notranslate">import numpy as np
import matplotlib.pyplot as plt

z = np.arange(150)
z.shape = (10,15)

fig, axs = plt.subplots(2,2)

ax = axs[0,0]
cs0 = ax.contourf(z, 20)
cbar0 = fig.colorbar(cs0, ax=ax)

ax = axs[0,1]
cs1 = ax.contourf(z, 20, alpha=0.3)
cbar1 = fig.colorbar(cs1, ax=ax)

ax = axs[1,0]
im2 = ax.imshow(z, interpolation='nearest')
cbar2 = fig.colorbar(im2, ax=ax)

ax = axs[1,1]
im3 = ax.imshow(z, interpolation='nearest', alpha=0.3)
cbar3 = fig.colorbar(im3, ax=ax)

plt.savefig("test1.pdf")
plt.savefig("test1.svg")
plt.show()
</pre>