gpt4 book ai didi

python - Shapely MultiLinestring 在 Pyplot 中显示为 MultiPolygon

转载 作者:行者123 更新时间:2023-12-01 01:46:22 54 4
gpt4 key购买 nike

目标:在同一个 pyplot 图上绘制 MultiPolygon(陆地)和 MultiLinestring(河流)。将土地涂成白色。

问题:似乎多线串显示为通过自动关闭所有线串以使它们成为多边形而构建的多多边形

Telltale:将 MultiPolygon 着色为白色时,它不会为看似由 MultiLinestring 中的 Linestring 制成的多边形着色

这是可重现的代码:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
from cartopy.feature import ShapelyFeature

# creates a map
map_projection = ccrs.PlateCarree(central_longitude=0.0, globe=None)
map_figure = plt.figure()
map_subplot = plt.axes(projection=map_projection)
# limits the display bounds of the map
map_subplot.set_extent((5.2, 31.4, 35, 54.3), crs=map_projection)

# adding land from a local shp (source : Natural Earth website)
# facecolor = white
landshpfilename = "Central Europe _ lands minus lakes.shp"
landshapereader = shpreader.Reader(landshpfilename)
landshape_feature = ShapelyFeature(landshapereader.geometries(), map_projection, facecolor='white',edgecolor='black')
map_subplot.add_feature(landshape_feature)

# adding large river from a local shp (source : Natural Earth website)
# edgecolor = blue
largeriversshpfilename = "Central Europe _ large rivers minus lakes.shp"
largeriversshapereader = shpreader.Reader(largeriversshpfilename)
largeriversshape_feature = ShapelyFeature(largeriversshapereader.geometries(), map_projection,edgecolor='blue')
map_subplot.add_feature(largeriversshape_feature)

# verifying the geom_type of the first objects in the shapefiles
# putting it as a title
land_geom_type_text = ' '.join(['lands geom_type :',next(landshape_feature.geometries()).geom_type])
river_geom_type_text = ' '.join(['rivers geom_type :',next(largeriversshape_feature.geometries()).geom_type])
map_figure.suptitle('\n'.join([land_geom_type_text,river_geom_type_text]))

plt.show()

结果如下: rendered map

问题:如何解决?

最佳答案

设置facecolor='none'将阻止 matplotlib 填充生成的底层路径。一个快速可重现的案例:

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


ax = plt.axes(projection=ccrs.PlateCarree())

# Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
states_provinces = cfeature.NaturalEarthFeature(
category='physical',
name='rivers_lake_centerlines',
scale='110m',
)

ax.add_feature(states_provinces, edgecolor='gray', linewidth=5)
ax.coastlines()

Filled rivers, booo!

保持其他所有内容相同,但更改特征构造以设置不设置面颜色会产生所需的结果:

states_provinces = cfeature.NaturalEarthFeature(
category='physical',
name='rivers_lake_centerlines',
scale='110m',
facecolor='none'
)

Rivers, yay!

关于python - Shapely MultiLinestring 在 Pyplot 中显示为 MultiPolygon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51289751/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com