2017-07-07 122 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