title: Matplotlib: filled contour plot with transparent colors
body: <p>Does somebody know – in Matplotlib – how to produce a good-looking filled contour plot with semi-transparent colors? If <code>contourf()</code> is passed a colormap with semi-transparent colors, it produces small gaps between the filled areas:</p>
  
  <p><a href="https://i.stack.imgur.com/eEQXI.png"><img src="https://i.stack.imgur.com/eEQXI.png" alt="small gaps between areas"></a></p>
  
  <p>According to the <a href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.contourf">docs</a>, this is not a bug ("<code>contourf()</code> [...] does not draw the polygon edges"). To draw the edges, it is suggested to "add line contours with calls to <code>contour()</code>". But that doesn't look good either as the edges become too opaque:</p>
  
  <p><a href="https://i.stack.imgur.com/s17F9.png"><img src="https://i.stack.imgur.com/s17F9.png" alt="edges are too opaque"></a></p>
  
  <p>You can play around with the <code>linewidth</code> argument of <code>contour()</code>, but that doesn't help much. Any ideas?</p>
  
  <p>Here's the code that reproduces the problem (I use the object-oriented API, but the result is the same with <code>pyplot</code>):</p>
  
  <pre class="lang-python prettyprint-override"><code>import matplotlib
  import numpy as np
  from matplotlib.figure import Figure
  from matplotlib.backends.backend_agg import FigureCanvasAgg
  
  # generate some data
  shape = (100, 100)
  x_rng = np.linspace(-1, 1, shape[1])
  y_rng = np.linspace(-1, 1, shape[0])
  x, y = np.meshgrid(x_rng, y_rng)
  z = np.sqrt(x**2 + y**2)
  
  # create figure
  width_inch, height_inch = 5, 5  # results in 500x500px with dpi=100
  fig = Figure()
  fig.set_size_inches((width_inch, height_inch))
  FigureCanvasAgg(fig)
  ax = fig.add_axes([0., 0., 1., 1.])
  ax.set_axis_off()
  
  # define some colors with alpha &lt; 1
  alpha = 0.9
  colors = [
      (0.1, 0.1, 0.5, alpha),  # dark blue
      (0.0, 0.7, 0.3, alpha),  # green
      (0.9, 0.2, 0.7, alpha),  # pink
      (0.0, 0.0, 0.0, alpha),  # black
      (0.1, 0.7, 0.7, alpha),  # light blue
  ]
  cmap = matplotlib.colors.ListedColormap(colors)
  levels = np.array(np.linspace(0, z.max(), len(colors)))
  norm = matplotlib.colors.BoundaryNorm(levels, ncolors=cmap.N)
  
  # contourf plot produces small gaps between filled areas
  cnt = ax.contourf(x, y, z, levels, cmap=cmap, norm=norm,
                    antialiased=True, linecolor='none')
  
  # this fills the gaps, but it makes them too opaque
  # ax.contour(x, y, z, levels, cmap=cmap, norm=norm,
  #            antialiased=True)
  
  # the same is true for this trick:
  # for c in cnt.collections:
  #     c.set_edgecolor("face")
  
  filename = "/tmp/contourf.png"
  fig.savefig(filename, dpi=100, transparent=True, format="png")
  </code></pre>
  
  <p>PS: The same plot looks good with the SVG backend.</p>
  
  <p>PPS: <code>pcolormesh()</code> has a similar problem:</p>
  
  <pre class="lang-python prettyprint-override"><code>ax.pcolormesh(x, y, z, cmap=cmap, norm=norm,
                edgecolor="face", antialiased=True)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Gbwcb.png"><img src="https://i.stack.imgur.com/Gbwcb.png" alt="enter image description here"></a></p>