7

Using basemap I used to add my custom boundary shapefile like this:

map = Basemap(..)
map.readshapefile(file.shp, 'attribute', drawbounds=True)

How can I do the same using cartopy?

I tried this:

ax.add_feature(cfeature.shapereader.Polygon('file.shp'))

but that's not working..

4

1 回答 1

15

目前没有 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()

或者,您可以只使用 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()

孵化的冰川区域

高温高压

于 2014-01-08T09:25:39.910 回答