Select 绘制 shapefile 时的某些索引

Select certain indices when plotting shapefiles

我的问题

shapefile xxx 包含多个多边形,我想绘制其中的一部分。
所以,我输入:

## reading the xxx.shp
map.readshapefile('xxx','xxx',zorder =1,)
patches=[]

## generating the indice which mean I want to plot these polygon.
indice = [int(i) for i in np.array([5,6,7,8,11,14,17])]

for info, shape in zip(map.xxx_info, map.xxx):
    x,y=zip(*shape)
    patches.append( Polygon(np.array(shape), True) )      
ax.add_collection(PatchCollection(patches[indice], facecolor= \
                  'none',edgecolor='grey', linewidths=1.5, zorder=2,alpha = 0.8))       

但是错误是这样的:

TypeError: llist indices must be integers, not list.

我不知道如何修复它。希望得到您的帮助!

如果仔细观察,您会发现您正试图在下一行中使用 indice 作为 patches 的索引。

ax.add_collection(PatchCollection(patches[indice], facecolor= \
              'none',edgecolor='grey', linewidths=1.5, zorder=2,alpha = 0.8))

问题是 indice 是一个 list,不能以这种方式使用。

从您的代码来看,您似乎正在尝试为 指定索引创建补丁。为此,我将创建一个新的补丁列表,它是 patches 中包含的补丁的子集。然后使用此子集创建您的 PatchCollection.

somepatches = [patch for k,patch in enumerate(patches) if k in indice]
collection = PatchCollection(somepatches, facecolor='none',
                  edgecolor='grey', linewidths=1.5, zorder=2, alpha=0.8)
ax.add_collection(collection)