title: Python: how to create a choropleth map out of a shapefile of Canada?
body: <p>My goal here is to create a <a href="https://en.wikipedia.org/wiki/Choropleth_map" rel="noreferrer">choropleth map</a> of Canada in Python. Suppose I have a dictionary with values referring to each Canadian province/territory:</p>
  
  <pre><code>myvalues={'Alberta': 1.0,
   'British Columbia': 2.0,
   'Manitoba': 3.0,
   'New Brunswick': 4.0,
   'Newfoundland and Labrador': 5.0,
   'Northwest Territories': 6.0,
   'Nova Scotia': 7.0,
   'Nunavut': 8.0,
   'Ontario': 9.0,
   'Prince Edward Island': 10.0,
   'Quebec': 11.0,
   'Saskatchewan': 12.0,
   'Yukon': 13.0}
  </code></pre>
  
  <p>Now I want to color each province based on the corresponding value in <code>myvalues</code>, using a continuous colormap (e.g., shades of red). <strong>How to do that?</strong></p>
  
  <p>So far I have only been able to plot the Canadian provinces/territory within matplotlib, but their shapes appear in a unique color, and I don't know how to change that according to the numbers in <code>myvalues</code> (maybe I need to play with <code>patches</code> but I don't know how).</p>
  
  <p>This is where you can find the shapefile: <a href="http://www.filedropper.com/canadm1_1" rel="noreferrer">http://www.filedropper.com/canadm1_1</a></p>
  
  <p>And this is my code to date:</p>
  
  <pre><code>import shapefile
  import matplotlib.pyplot as plt
  import matplotlib.patches as patches
  from matplotlib.patches import Polygon
  from matplotlib.collections import PatchCollection
  #   -- input --
  sf = shapefile.Reader("myfolder\CAN_adm1.shp")
  recs    = sf.records()
  shapes  = sf.shapes()
  Nshp    = len(shapes)
  cns     = []
  for nshp in xrange(Nshp):
      cns.append(recs[nshp][1])
  cns = array(cns)
  cm    = get_cmap('Dark2')
  cccol = cm(1.*arange(Nshp)/Nshp)
  #   -- plot --
  fig     = plt.figure()
  ax      = fig.add_subplot(111)
  for nshp in xrange(Nshp):
      ptchs   = []
      pts     = array(shapes[nshp].points)
      prt     = shapes[nshp].parts
      par     = list(prt) + [pts.shape[0]]
      for pij in xrange(len(prt)):
       ptchs.append(Polygon(pts[par[pij]:par[pij+1]]))
      ax.add_collection(PatchCollection(ptchs,facecolor=None,edgecolor='k', linewidths=.5))
  ax.set_xlim(-160,-40)
  ax.set_ylim(40,90)
  </code></pre>
  
  <p>This is the image I am getting so far:</p>
  
  <p><a href="https://i.stack.imgur.com/RMkmZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/RMkmZ.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I get the solution must be in the following lines:</p>
  
  <pre><code>cm    = get_cmap('OrRd')
  cccol = cm(1.*arange(Nshp)/Nshp)
  </code></pre>
  
  <p>The above script creates a <code>cccol</code> array which in reality has this shape:</p>
  
  <pre><code>array([[ 1.        ,  0.96862745,  0.9254902 ,  1.        ],
         [ 0.99766244,  0.93356402,  0.84133796,  1.        ],
         [ 0.99520185,  0.89227221,  0.74749713,  1.        ],
         [ 0.99274125,  0.84306037,  0.64415227,  1.        ],
         [ 0.99215686,  0.78754327,  0.5740254 ,  1.        ],
         [ 0.99186467,  0.71989237,  0.50508269,  1.        ],
         [ 0.98940408,  0.60670514,  0.39927722,  1.        ],
         [ 0.97304114,  0.50618995,  0.32915034,  1.        ],
         [ 0.94105344,  0.40776625,  0.28732027,  1.        ],
         [ 0.88521339,  0.28115341,  0.19344868,  1.        ],
         [ 0.8220992 ,  0.16018455,  0.10345252,  1.        ],
         [ 0.73351789,  0.04207613,  0.02717416,  1.        ],
         [ 0.61959248,  0.        ,  0.        ,  1.        ]])
  </code></pre>
  
  <p>I don't know why it has 4 columns, but I figure that if I can somehow link the values of this array to those specified in the <code>values</code> dict, I can solve the problem. Any ideas?</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I have figured out the "trick" is in <code>cccol = cm()</code>. In order to relate this to the provinces, I tried to assign 
  <code>cccol = cm(myvalues.values(i) for i in myvalues.keys())</code></p>
  
  <p>so that (in my mind at least) each color is assigned based on the relevant key and there are no misplacements. The problem is that I get an error: </p>
  
  <p><code>TypeError: Cannot cast array data from dtype('O') to dtype('int32') according to the rule 'safe'</code>. </p>
  
  <p>How to work around this?</p>