Ben*_*ris 13 python gis matplotlib
我想生成一张美国地图,并使用不同的阴影为每个州着色.有没有办法使用Python的底图?
Wes*_*ton 18
在GitHub上的Basemap repo中有一个很好的格式化示例:fillstates.py.shapefile(dbf | shp | shx)也包含在examples文件夹中.
以下是该示例的缩写版本:
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
# create the map
map = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49,
projection='lcc',lat_1=33,lat_2=45,lon_0=-95)
# load the shapefile, use the name 'states'
map.readshapefile('st99_d00', name='states', drawbounds=True)
# collect the state names from the shapefile attributes so we can
# look up the shape obect for a state by it's name
state_names = []
for shape_dict in map.states_info:
state_names.append(shape_dict['NAME'])
ax = plt.gca() # get current axes instance
# get Texas and draw the filled polygon
seg = map.states[state_names.index('Texas')]
poly = Polygon(seg, facecolor='red',edgecolor='red')
ax.add_patch(poly)
plt.show()
Run Code Online (Sandbox Code Playgroud)
德克萨斯州红色的结果情节:

需要注意的是,当我们加载一个shape文件的形状和属性被存储在map.states和map.states_info分别为列表基础上,name在使用的参数readshapefile调用.因此,要查找特定状态的形状,我们必须从属性中构建相应的状态名称列表.
| 归档时间: |
|
| 查看次数: |
15041 次 |
| 最近记录: |