2013-10-18 59 views
3

我有下面的代码添加一个多边形到一个情节:使用matplotlib-底图

from mpl_toolkits.basemap import Basemap 
map = Basemap(projection='merc', lat_0=50, lon_0=4, 
    resolution = 'l', area_thresh = 0.1, 
    llcrnrlon=4, llcrnrlat=50, 
    urcrnrlon=40, urcrnrlat=60) 

map.drawcoastlines(linewidth=0.15) 
map.drawcountries(linewidth=0.15) 
map.fillcontinents(color='brown',lake_color='white') 
map.drawmapboundary(fill_color='white') 

enter image description here

并在此地图我想显示仅由一个多边形shape文件的顶部。多边形定义了一个封闭区域。我已经找到了关于如何手动添加多边形或从shapefile中绘制多个多边形的不同教程,但我无法为我的情况做到这一点。 shapefile属性表仅由两个字段组成:'c'和'区域'。

现在我已经到了这个

import shapefile 

s = shapefile.Reader(filepath,'c',drawbounds=False) 
shapes = s.shapes() 
records = s.records() 
for record, shape in zip(records,shapes): 
    lons,lats = zip(*shape.points) 
    data = np.array(map(lons, lats)).T 
x, y =map(lons,lats) 
+0

看到这个答案HTTP ://stackoverflow.com/a/19444121/380231如何构建一个多边形。 – tacaswell

回答

1

有同样的问题,但它是如此简单,你从来​​没有想过与许多教程和模块和一种类似的问题在网上做:

map.readshapefile('luthuania', 'any_name_you_like', drawbounds=True) 

所以你的例子:

from mpl_toolkits.basemap import Basemap 
map = Basemap(projection='merc', lat_0=50, lon_0=4, 
    resolution = 'l', area_thresh = 0.1, 
    llcrnrlon=4, llcrnrlat=50, 
    urcrnrlon=40, urcrnrlat=60) 

map.readshapefile('luthuania', 'any_name_you_like', drawbounds=True, linewidth=2, color='b') 

map.drawcoastlines(linewidth=0.15) 
map.drawcountries(linewidth=0.15) 
map.fillcontinents(color='brown',lake_color='white') 
map.drawmapboundary(fill_color='white') 

其中给出

lithuania

模块shape文件,顺便在引擎盖下使用底图:看C:\ Python33 \ LIB \站点包\ mpl_toolkits \底图\ shapefile.py