title: How do you create a legend for a contour plot in matplotlib?
body: <p>I can't seem to find the answer anywhere! I found a discussion <a href="http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg08735.html" rel="noreferrer">here</a>, but trying this I get a <code>TypeError: 'NoneType' object is not iterable</code>:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; import matplotlib.pyplot as plt
  &gt;&gt;&gt; x, y = np.meshgrid(np.arange(10),np.arange(10))
  &gt;&gt;&gt; z = x + y
  &gt;&gt;&gt; cs = plt.contourf(x,y,z,levels=[2,3])
  &gt;&gt;&gt; cs.collections[0].set_label('test')
  &gt;&gt;&gt; plt.legend()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2791, in legend
      ret =  gca().legend(*args, **kwargs)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 4475, in legend
      self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend.py", line 365, in __init__
      self._init_legend_box(handles, labels)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend.py", line 627, in _init_legend_box
      handlebox)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend_handler.py", line 110, in __call__
      handlebox.get_transform())
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend_handler.py", line 352, in create_artists
      width, height, fontsize)
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend_handler.py", line 307, in get_sizes
      size_max = max(orig_handle.get_sizes())*legend.markerscale**2
  TypeError: 'NoneType' object is not iterable
  </code></pre>
  
  <p><strong>EDIT</strong>: I'm looking for something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/Eyxwx.png" alt="kamland solar delta chi-squared map"></p>