使用geoplot(python)在地图上以多多边形的形状绘制

2024-09-28 19:27:42 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:

我有一个多边形,我想用geoplot在地图上绘制。当我尝试这样做时,我得到ValueError:输入GeoDataFrame没有“geometry”列集

如何正确设置多重多边形的格式(可能是规则形状多边形的类似问题),以便在地图上正确绘制

当前代码:

from polygon_geohasher.polygon_geohasher import polygon_to_geohashes, geohashes_to_polygon
from shapely.geometry import Polygon
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs

# Create multipolygon consisting of two disconnected polygons:
a=geohashes_to_polygon('9v','9x')

# Plot map underlay:
contiguous_usa = gpd.read_file(gplt.datasets.get_path('contiguous_usa'))
ax=gplt.polyplot(contiguous_usa,
                 projection=gcrs.AlbersEqualArea(),
                 edgecolor='white',
                 facecolor='lightgray',
                 figsize=(12,12),
                )

# Add layer with multipolygon in red:
gplt.polyplot(a, ax=ax,facecolor='red',edgecolor='none') # This is the line that causes me problems
# OUTPUT: "ValueError: The input GeoDataFrame does not have a "geometry" column set."

Map of US without red layer

编辑:

将有问题的代码行更改为以下代码可以解决错误,但会导致地图放大红色图层:

gplt.polyplot(gpd.GeoSeries([a]), ax=ax,facecolor='red',edgecolor='none',alpha=.4)

Image zooms in on the map


Tags: to代码importas地图ax多边形geometry