title: Correcting matplotlib colorbar ticks
body: <p>I've placed a color bar alongside a choropleth map. Because the data being plotted are discrete rather than continuous values, I've used a LinearSegmentedColormap (using <a href="http://wiki.scipy.org/Cookbook/Matplotlib/ColormapTransformations" rel="noreferrer">the recipe from the scipy cookbook</a>), which I've initialised with my max counted value + 1, in order to show a colour for 0. However, I now have two problems:</p>
  <p><img src="https://i.stack.imgur.com/ru9jy.png" alt="enter image description here" /></p>
  <ol>
  <li><p>The tick labels are incorrectly spaced (except for 5, more or less) – they should be located in the middle of the colour they identify; i.e. 0 - 4 should be moved up, and 6 - 10 should be moved down.</p>
  </li>
  <li><p>If I initialise the colorbar with <code>drawedges=True</code>, so that I can style its <code>dividers</code> properties, I get this:</p>
  </li>
  </ol>
  <p><img src="https://i.stack.imgur.com/F15vJ.png" alt="enter image description here" /></p>
  <p>I'm creating my colormap and colorbar like so:</p>
  <pre><code>cbmin, cbmax = min(counts), max(counts)
  # this normalises the counts to a 0,1 interval
  counts /= np.max(np.abs(counts), axis=0)
  # density is a discrete number, so we have to use a discrete color ramp/bar
  cm = cmap_discretize(plt.get_cmap('YlGnBu'), int(cbmax) + 1)
  mappable = plt.cm.ScalarMappable(cmap=cm)
  mappable.set_array(counts)
  # set min and max values for the colour bar ticks
  mappable.set_clim(cbmin, cbmax)
  pc = PatchCollection(patches, match_original=True)
  # impose our colour map onto the patch collection
  pc.set_facecolor(cm(counts))
  ax.add_collection(pc,)
  cb = plt.colorbar(mappable, drawedges=True)
  </code></pre>
  <p>So I'm wondering whether my converting the counts to a 0,1 interval is one of the problems.</p>
  <h3>Update :</h3>
  <p>Having tried what Hooked suggested, the 0-value is correct, but subsequent values are set progressively higher, to the the point where 9 is where 10 should be:</p>
  <p><img src="https://i.stack.imgur.com/ZWznj.png" alt="enter image description here" /></p>
  <p>Here's the code I used:</p>
  <pre><code>cb = plt.colorbar(mappable)
  labels = np.arange(0, int(cbmax) + 1, 1)
  loc = labels + .5
  cb.set_ticks(loc)
  cb.set_ticklabels(labels)
  </code></pre>
  <p>And just to confirm, <code>labels</code> definitely has the correct values:</p>
  <pre><code>In [3]: np.arange(0, int(cbmax) + 1, 1)
  Out[3]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
  </code></pre>