目的:在同一pyplot图上绘制MultiPolygon(焊盘)和MultiLinestring(河流)。将土地着色为白色。
问题:似乎MultiLinestring显示为通过自动关闭其所有Linestring使其成为Polygons而构建的MultiPolygon
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()
保持其他所有条件不变,但是将特征构造更改为不设置任何面色将产生所需的结果:
states_provinces = cfeature.NaturalEarthFeature(
category='physical',
name='rivers_lake_centerlines',
scale='110m',
facecolor='none'
)