title: Plotting Shapefiles which color decided by the scatter point data within
body: <h3>My question</h3>
  
  <p>Plotting heatmap with shapefile</p>
  
  <h3>1.intro</h3>
  
  <ul>
  <li>a bunch of shapefile represent the administration boundaries</li>
  <li>a pandas.Dataframe containing some point with (longitude,latitude,value)     </li>
  </ul>
  
  <p>Code here:      </p>
  
  <pre><code> map = Basemap(llcrnrlon=xc1,llcrnrlat=yc1,urcrnrlon=xc2,urcrnrlat=yc2)
   ##Assuming "shape.shp" is my shapefile
   map.readshapefile('./shape','shape',zorder =1,)
   patches=[]
   cs=plt.cm.Greens(np.arange(18)/18.)
   for info, shape in zip(map.shape_info, map.shape):
       x,y=zip(*shape)
       patches.append( Polygon(np.array(shape), True) )  # facecolor= '#6582B3'
   ax.add_collection(PatchCollection(patches, facecolor= cs,edgecolor='none',     
                     linewidths=1.5, zorder=2))
  
   ## scatter the point, assuming "pt" is the Dataframe
   pt_lat = pt.lat.as_matrix()
   pt_lon   = power.lon.as_matrix()
   plt.scatter(pt_lon,pt_lat,marker='o',s=50,lw= 0,zorder = 3, alpha = 0.75)
  </code></pre>
  
  <p>Picture here:       </p>
  
  <p><a href="http://i11.tietuku.com/9785abb6097b7c0e.png" rel="nofollow noreferrer">http://i11.tietuku.com/9785abb6097b7c0e.png</a>   </p>
  
  <h3>2. My target</h3>
  
  <p>In the picture upward, the color of each shapefile is based on the colormap predefined.      </p>
  
  <blockquote>
    <ul>
    <li>Plotting Each area (In my case, 18 shapefile) with the color corresponding to the sum of pt.values within.        </li>
    <li>In other words, the inner point data decide the shapefile's color</li>
    </ul>
  </blockquote>
  
  <h3>Add --2015-01-11</h3>
  
  <p>Thanks for @MaxNoe's answer.     </p>
  
  <p>I have learned from your code, but still has some problem.<br>
  Here is my code &amp; picture:     </p>
  
  <pre><code> fig = plt.figure(figsize =(8,6))
   ax = plt.subplot()
   map = Basemap(llcrnrlon=xc1,llcrnrlat=yc1,urcrnrlon=xc2,urcrnrlat=yc2) 
   map.readshapefile('./shape','shape')
  
   patches=[]
   for info, shape in zip(map.shape_info, map.shape):
       x,y=zip(*shape)
   patches.append(Polygon(np.array(shape), True) )
  
   xx = pt.lon.iloc[:].as_matrix()
   yy = pt.lat.iloc[:].as_matrix()
   value = pt.value.iloc[:].as_matrix()
  
   sh = (len(xx),2)
   position = np.zeros(len(xx)*2).reshape(*sh)
   for i in range(0,len(xx),1):
       position[i] = np.array([xx[i],yy[i]])
  
   poly_values = []
   for patch in patches:
       mask = np.array([patch.contains_point(xy) for xy in position])
       poly_values.append(value[mask].sum())
  
   coll = PatchCollection(patches, cmap = 'Greens')
   coll.set_array(np.array(poly_values))
   ax.add_collection(coll)
   plt.colorbar(coll,label = "polygon")
  
   point_plot = plt.scatter(xx,yy,marker='o',s=80,lw= 0,zorder = 3, c = "r",alpha = 0.75)
  
   ax.set_frame_on(False)
   divider = make_axes_locatable(ax)
   cax = divider.append_axes("right", size="4%", pad=0.1)
   cbar = plt.colorbar(coll,label = "polygon",cax= cax)
  </code></pre>
  
  <p><a href="http://i4.tietuku.com/9a7b0cbc16f2e0b0.png" rel="nofollow noreferrer">http://i4.tietuku.com/9a7b0cbc16f2e0b0.png</a></p>
  
  <blockquote>
    <ul>
    <li>It seems like the color for polygon[i] isn't according to the poly_value[i]</li>
    <li>I think the problem is <code>coll.set_array</code> doesn't work.</li>
    <li>Otherwise, I have checked each polygon and the scatter point value within, the poly_value[i] and the actual condition is not match(bigger than reality). I think the I may use value.mask wrong.     </li>
    </ul>
  </blockquote>