title: Legend with colors for patchcollection
body: <p>I have patches that are colored by a scalar value (integer). Now I'd like to create a legend giving names to the patches of a certain color/integer value. Here is what I tried so far:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  from matplotlib.patches import Polygon
  from matplotlib.collections import PatchCollection
  import numpy as np
  
  fig = plt.figure(figsize=(10,8))
  ax = fig.add_subplot(111)
  
  patches = []
  cvect = []
  for kx in range(10):
      for ky in range(10):
          patches.append(Polygon([(kx,ky),(kx,ky+1),(kx+1,ky+1),(kx+1,ky)]))
          cvect.append(((kx*ky)%6))
  
  cmap = plt.cm.get_cmap('jet')
  
  
  pc = PatchCollection(patches,edgecolors='none',cmap=cmap)
  pc.set_array(np.array(cvect))
  ax.add_collection(pc)
  
  clist = list(set(cvect))
  handles = []
  for col in clist:
      handles.append(Polygon([(0,0),(10,0),(0,-10)],color=cmap(col),
                             label='Material %i'%(col)))
  
  plt.legend(handles=handles)
  
  ax.set_xlim([0,10])
  ax.set_ylim([0,10])
  
  fig.savefig('fig')
  plt.close(fig)
  </code></pre>
  
  <p>But the colors in the legend do not match the colors of the patches with the same integer value. What am I doing wrong?</p>
  
  <p><a href="https://i.stack.imgur.com/YFb8y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YFb8y.png" alt="enter image description here"></a></p>