Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Cartopy:用于绘制特定河流的子集要素_Python_Python 3.x_Cartopy - Fatal编程技术网

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')