title: How to add legend to imshow() in matplotlib
body: <p>I am using <code>matplotlib</code></p>
  
  <p>In <code>plot()</code> or <code>bar()</code>, we can easily put legend, if we add labels to them. but what if it is a <code>contourf()</code> or <code>imshow()</code></p>
  
  <p>I know there is a <code>colorbar()</code> which can present the color range, but it is not satisfied. I want such a legend which have names(labels).</p>
  
  <p><img src="https://i.stack.imgur.com/eoHLT.png" alt="enter image description here"></p>
  
  <p>For what I can think of is that, add  labels to each element in the matrix, then ,try legend(), to see if it works, but how to add label to the element, like a value??</p>
  
  <p>in my case, the raw data is like:</p>
  
  <pre><code>1,2,3,3,4
  2,3,4,4,5
  1,1,1,2,2
  </code></pre>
  
  <p>for example, 1 represents 'grass', 2 represents 'sand', 3 represents 'hill'... and so on.
  imshow() works perfectly with my case, but without the legend.</p>
  
  <p>my question is:</p>
  
  <ol>
  <li><p>Is there a function that can automatically add legend, for example, in my case, I just have to do like this: someFunction('grass','sand',...)</p></li>
  <li><p>If there isn't, how do I add labels to each value in the matrix. For example, label all the 1 in the matrix 'grass', labell all the 2 in the matrix 'sand'...and so on.</p></li>
  </ol>
  
  <p>Thank you!</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>Thanks to @dnalow, it's smart really. However, I still wonder if there is any formal solution.</p>