我正在寻找一种基于值填充形状文件的多边形的方法。 从basemap教程(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()
我想做的是从这样的字典中获取值:
^{pr2}$其中的键是shapefile中的info['ID_2']列,如上面的代码所示,值是我要表示为color的变量。这意味着拥有一个从1.16到1.81的颜色映射,并且每个多边形(ID_2)都有一个与其dict1中的值相关的颜色。在
提前谢谢
您似乎想在basemap中生成一个choropleth图。
为此,您需要一个colormap
cmap
和一个规范化norm
,以便将值映射到颜色,cmap(norm(val))
。对于每个形状,可以将Polygon
的颜色设置为字典中相应的颜色,在本例中是cmap(norm(dict1[info['ID_2']]))
。在在
PatchCollection
内,match_original=True
需要设置以保持原始多边形的颜色。在最后,从colormap和规范化生成一个colormap可能是有用的。在
相关问题 更多 >
编程相关推荐