Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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 在Pyplot中,一条形状优美的多边线显示为一条多边形_Python_Matplotlib_Shapely_Cartopy - Fatal编程技术网

Python 在Pyplot中,一条形状优美的多边线显示为一条多边形

Python 在Pyplot中,一条形状优美的多边线显示为一条多边形,python,matplotlib,shapely,cartopy,Python,Matplotlib,Shapely,Cartopy,目标:在同一个pyplot图形上绘制多多边形(陆地)和多边界(河流)。将陆地涂成白色 问题:多行线似乎显示为一个多多边形,通过自动关闭其所有线串使其成为多边形而构建 说明:将多边形着色为白色时,它不会将似乎是由多边形线串组成的多边形着色 以下是可复制的代码: import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.io.shapereader as shpreader from cartopy.featu

目标:在同一个pyplot图形上绘制多多边形(陆地)和多边界(河流)。将陆地涂成白色

问题:多行线似乎显示为一个多多边形,通过自动关闭其所有线串使其成为多边形而构建

说明:将多边形着色为白色时,它不会将似乎是由多边形线串组成的多边形着色

以下是可复制的代码:

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()
结果如下:


问题:如何修复?

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

我们无法访问您在代码中引用的.shp文件。可以将它们更改为简单的geojson引用吗?你可以阅读形状文件(并将其转换为json格式),答案是“cartopy可能会以某种方式将几何体转换为多边形,然后对其进行适当的着色”。@Micks Ketches我认为形状文件在这里不相关,但请注意,它们是“陆地”和“河流+湖泊中心线”的剪辑。另外,看到gepcel的答案,我想我必须找到另一种绘制它们的方法,例如geojson或wkt。@gepcel感谢你的答案。我从佩尔森的回答中了解到,我的问题暂时无法解决。你的问题和答案可以追溯到2014年。从那以后你有没有向Cartopy项目团队提交过问题通知单?@Tehem没有,我没有提交过任何问题。当时,我正在寻找一种绘制点的方法,最终我使用了
plt.scatter
。我通过给河流多边形与陆地相同的面色,成功地获得了正确的外观。在我的例子中,我将两个
“白色”
,河流多边形不显示。我认为您的解决方案,
'none'
(而不是像我之前尝试的那样
none
),效果更好,因为它是自适应的。但是,我担心在检查点是在河流线串上方还是在陆地多边形内部时会遇到一些问题,因为河流顶部仍然有自己的多边形。我以后再谈。