我正在使用底图和shapefile创建覆盖底图的多边形。一切都很好,但是现在我想根据1到100之间的列表中的值,用不同的绿色阴影填充多边形。
我有一个如下所示的元组排序列表:
kk = Counter(table["county"]).items()
## sort based on the second item in the tuple
kk = sorted(kk,key=lambda x: x[1])
kk
[('EL DORADO',1),('SAN FRANCISCO',('SACRAMENTO',2),('SAN MATEO',3),('SHASTA',('ALAMEDA',5),('CONTRA COSTA',('HUMBOLDT',6),('PLUMAS',('SOLANO',9),('LOS ANGELES',10),('SAN BENITO',18)...]
我使用以下功能分离元组并分别访问其值:
def separateSortedValues(arg):
s_x = []
s_y = []
for x in kk:
s_x.append(x[0])
s_y.append(x[1])
return([s_x,s_y])
这是我的代码,其中我在shapefile中进行迭代,以查找具有我要查找的县名的多边形。如果是这样,则将它们添加到名为patches
的列表中。 patches
然后传递到PatchCollection
,在这里我可以控制要分配的颜色。
def fill_counties(table_data):
patches = []
counties = table_data[0]
for info,shape in zip(map.counties_info,map.counties):
if info["NAME"] in counties:
patches.append(Polygon(np.array(shape),True))
filled_polygons = PatchCollection(patches,facecolor="green",edgecolor="gray",linewidths=.5,zorder=2)
return(ax.add_collection(filled_polygons))
fill_counties(separateSortedValues(kk))
我希望能够根据与县相关联的table[1]
值使每个Polygon变成不同的绿色,因为我不认为为整个PatchCollection facevalue
着色一种颜色是正确的要走的路。