如何使用cartopy添加自定义shapefile以进行映射

Mat*_*ijn 6 cartopy

使用底图我用来添加我的自定义边界shapefile,如下所示:

map = Basemap(..)
map.readshapefile(file.shp, 'attribute', drawbounds=True)
Run Code Online (Sandbox Code Playgroud)

我怎样才能使用cartopy做同样的事情?

我试过这个:

ax.add_feature(cfeature.shapereader.Polygon('file.shp'))
Run Code Online (Sandbox Code Playgroud)

但那不起作用..

pel*_*son 11

目前没有ShapefileFeature类(虽然这很容易创建,并且可能很有意义)所以如果你真的想要使用功能界面,那么就有一个跳过:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature

fname = '50m_glaciated_areas.shp'

ax = plt.axes(projection=ccrs.Robinson())
shape_feature = ShapelyFeature(Reader(fname).geometries(),
                                ccrs.PlateCarree(), facecolor='none')
ax.add_feature(shape_feature)
plt.show()
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用add_geometries方法,该方法没有使用功能接口(因此,将来,将不会优化从磁盘读取实际绘制的几何图形,例如ShapefileFeature类):

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io.shapereader import Reader

fname = '50m_glaciated_areas.shp'

ax = plt.axes(projection=ccrs.Robinson())
ax.add_geometries(Reader(fname).geometries(),
                  ccrs.PlateCarree(),
                  facecolor='white', hatch='xxxx')
plt.show()
Run Code Online (Sandbox Code Playgroud)

孵出的冰川区域

HTH