我正在寻找一种基于值填充shapefile多边形的方法。到目前为止,距底图教程(http://basemaptutorial.readthedocs.io/en/latest/shapefile.html)为止,我发现了如何用特定颜色填充多边形。
import matplotlib.pyplot as plt
import pypyodbc
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.patches import PathPatch
import numpy as np
fig= plt.figure()
ax= fig.add_subplot(111)
m=Basemap(projection='cyl',llcrnrlat=34.5,llcrnrlon=19,urcrnrlat=42,urcrnrlon=28.5,resolution='h')
m.drawmapboundary(fill_color='aqua')
m.fillcontinents(color='#ddaa66',lake_color='aqua')
m.drawcoastlines()
m.readshapefile('nomoi','nomoi')
patches = []
for info, shape in zip(m.nomoi_info, m.nomoi):
if info['ID_2'] == 14426:
patches.append( Polygon(np.array(shape), True) )
ax.add_collection(PatchCollection(patches, facecolor='m', edgecolor='k', linewidths=1., zorder=2))
plt.show()
Run Code Online (Sandbox Code Playgroud)
我想做的是从这样的字典中获取值:
dict1={14464: 1.16, 14465: 1.35, 14466: 1.28, 14467: 1.69, 14468: 1.81, 14418: 1.38}
Run Code Online (Sandbox Code Playgroud)
其中的键是shapefile中的info ['ID_2']列(如上面的代码所示),值是我要表示为颜色的变量。表示颜色图的范围从1.16到1.81不等,并且每个多边形(ID_2)的颜色都与dict1中的值相关。
提前致谢