title: Plotting discrete colorbar in legend style using Matplotlib
body: <p>Sometimes, I want to plot discrete value in pcolormesh style.  </p>
  
  <p>For example, to represent a 2-d array in the shape of 100x100 which contain int 0~7</p>
  
  <pre><code>data  = np.random.randint(8, size=(100,100))
  cmap = plt.cm.get_cmap('PiYG', 8) 
  plt.pcolormesh(data,cmap = cmap,alpha = 0.75)
  plt.colorbar()  
  </code></pre>
  
  <p>The figure shows like this:<br>
  <a href="https://i.stack.imgur.com/kn1on.png" rel="noreferrer"><img src="https://i.stack.imgur.com/kn1on.png" alt="enter image description here"></a>   </p>
  
  <p>How to generate the colorbar in legend style. In other word, each color box corresponds to its value(e.g pink colorbox --> 0)  </p>
  
  <p>An illustration here(Not fit this example):  </p>
  
  <p><a href="https://i.stack.imgur.com/aRM4D.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aRM4D.png" alt="enter image description here"></a></p>