2017-07-07 120 views
0

Python3cartopy工作时,我正在尝试从Natureal Earth数据库中绘制一个特定的河流。Cartopy:绘制特定河流的子集特征

这是很简单的绘制所有河流和然后设置的程度在特定区域:

import matplotlib.pyplot as plt 
import cartopy.crs as ccrs 
import cartopy.feature 

rivers = cartopy.feature.NaturalEarthFeature(
    category='physical', name='rivers_lake_centerlines', 
    scale='10m', facecolor='none', edgecolor='blue') 

fig, ax = plt.subplots(
    nrows=1, ncols=1, subplot_kw={'projection': ccrs.PlateCarree()}, 
         figsize=(10,6)) 
ax.add_feature(rivers, linewidth=1) 
ax.set_extent([-65, -45, -40, -17.5]) 
plt.show() 

(如下所示结果)

但是,如果我希望仅绘制特定的河流(为了说明Paraná,由于编码问题在数据中被命名为Paran?,似乎没有明确的方式来做到这一点)The cartopy Feature interface documentation

回答

2

您需要使用cartopy.io.shapereader,这里是我的电脑上工作的代码:

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

#config # format-dict 

# assuming you have downloaded that file already using your original code 
# its full path name should be (Windows) 
fpath = config['data_dir'] + r'\shapefiles\natural_earth\physical\10m_rivers_lake_centerlines.shp' 

as_shp = shapereader.Reader(fpath) 

fig, ax = plt.subplots(nrows=1, ncols=1, \ 
         subplot_kw={'projection': ccrs.PlateCarree()}, \ 
         figsize=(10,6)) 

# plot some geometries, based on their attribs 
for rec in as_shp.records(): 
    if rec.attributes['name'] == 'Parana?ba': 
     ax.add_geometries([rec.geometry], ccrs.PlateCarree(), edgecolor='none', facecolor='blue') 
    pass 

ax.coastlines(resolution='110m') 
ax.set_extent([-65, -45, -40, -17.5]) 
plt.show() 

the output

+0

任何想法如何使河流绘制线条,而不是多边形? – mbarete

+0

愚蠢的问题 - 用'ax.add_geometries([rec.geometry],ccrs.PlateCarree(),edgecolor ='blue',facecolor ='none' – mbarete