-1

My problem

The shapefile xxx contain serval polygons, and I want to plot part of it.
So, I typed:

## 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))       

But the error like this:

TypeError: llist indices must be integers, not list.

I don't know how to fix it. Wish for yours' help!

Han Zhengzu
  • 3,694
  • 7
  • 44
  • 94

1 Answers1

1

If you look a little closer, you'll see that you're trying to use indice as an index into patches in the following line.

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

The issue is that indice is a list and can't be used in this way.

From your code, it looks like you are trying to create patches for only the specified indices. To do this, I would create a new list of patches that is a subset of the patches contained in patches. Then use this subset to create your 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)
Suever
  • 64,497
  • 14
  • 82
  • 101