Вопрос

I need to fill my polygon using a heatmap. For source of polygon I've use shapefile. This is my code:

import shapefile
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.cm as mcm
import matplotlib.image as mpimg 
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import pylab as plb   


fig     = plt.figure()
ax      = fig.add_subplot(111)
ax.set_frame_on(False)

sf = shapefile.Reader("./data/boundary-polygon")
recs    = sf.records()
shapes  = sf.shapes()
print shapes[1].__dict__
Nshp    = len(shapes)
cns     = []
for nshp in xrange(Nshp):
    cns.append(recs[nshp][1])
cns = np.array(cns)
cm    = mcm.get_cmap('Dark2')
cccol = cm(1.*np.arange(Nshp)/Nshp)
#   facecolor=cccol[nshp,:],

for nshp in xrange(Nshp):
    ptchs   = []
    pts     = np.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]], alpha=1))
    ax.add_collection(PatchCollection(ptchs,facecolors=((1, 1, 1, 1),),alpha=0.1 ,linewidths=1))
ax.set_xlim(54,67)
ax.set_ylim(50,57)

I want to change facecolors=((1, 1, 1, 1),) to facecolors=<image_of_my_heat_map>. Any help regarding this would be deeply appreciated.

Это было полезно?

Решение

Just set the polygons to be whatever color you want them to be:

ptchs=[]
for pij in xrange(len(prt)):
    ptchs.append(Polygon(pts[par[pij]:par[pij+1]], alpha=1, color=your_color))

and then create the PatchCollection with the warg match_orginal:

ax.add_collection(PatchCollection(ptchs, match_orginal=True, alpha=0.1 ,linewidths=1))

Also see Why is matplotlib.PatchCollection messing with color of the patches?

Другие советы

I am not familiar with the shapefile API for reading the vector data/polygons. I typically use OGR to read GIS vector data. The colour per polygon can be stored as an attribute per feature or just as a scalar which is assigned a colour using the colourmap as you have done here.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top