Python Cartopy:用于绘制特定河流的子集要素
我正在使用Python Cartopy:用于绘制特定河流的子集要素,python,python-3.x,cartopy,Python,Python 3.x,Cartopy,我正在使用Python3和cartopy,试图从自然地球数据库中对特定河流进行子集划分和绘制 绘制所有河流,然后设定特定区域的范围非常简单: import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature rivers = cartopy.feature.NaturalEarthFeature( category='physical', name='rivers_lake_centerl
Python3
和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?
),那么似乎没有明确的方法可以做到这一点
您需要使用
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()
您需要使用
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()
知道如何将河流绘制为直线而不是多边形吗?愚蠢的问题——用
ax修复。添加几何体([rec.geometry],ccrs.PlateCarree(),edgecolor='blue',facecolor='none')
知道如何将河流绘制为直线而不是多边形吗?愚蠢的问题——用ax.add\u几何体修复([rec.geometry],ccrs.PlateCarree(),edgecolor='blue',facecolor='none')