如何将 Geopandas 图放在 matplotlib pyplot 图像之上?

How to put a Geopandas plot on top of a matplotlib pyplot image?

目前,我有一个来自 cartopy 的某个地理区域的地块,我有一个来自 Geopandas 的几何对象,但无论我做什么,我都无法将它们组合成一个地块.两者的轴相同,我实际上可以将几何对象的轴放到地理区域上,但随后我的形状消失了。

这里是代码的相关部分:

from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.io.img_tiles as cimgt
import geopandas as gpd   
from matplotlib import pyplot as plt   
                
                
# Plotting the trade area graph
trade_geo_df = trade_area_response.json()['data']
trade_geo_df = gpd.GeoDataFrame(trade_geo_df)
trade_geo_df.loc[:, 'coordinates'] = trade_geo_df.coordinates.map(lambda x: x[0])
trade_geo_df['geometry'] = trade_geo_df.coordinates.apply(shapely.geometry.Polygon)
trade_geo_df['geometry'].plot()   
plt.plot(placer_lng, placer_lat, markersize=2, marker='o', color='red')  # Just a red dot

fig = plt.figure()
stamen_terrain = cimgt.Stamen('terrain-background')
        
# Limit the extent of the map to a small longitude/latitude range
ax = fig.add_subplot(1, 1, 1, projection=stamen_terrain.crs)
ax.set_extent([-89, -86, 41, 43], crs=ccrs.Geodetic())
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=2, color='gray', alpha=0.5, linestyle='--')
gl.ylabels_right = False
gl.xlabels_top = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 10, 'color': 'gray'}
gl.ylabel_style = {'size': 10, 'color': 'gray'}
            
ax.add_image(stamen_terrain, 8)
            
ax.plot(placer_lng, placer_lat, markersize=2, marker='o', color='red', transform=ccrs.Geodetic())  # Just another red dot
plt.show()

更多信息,以下是代码块中提到的trade_geo_df['geometry']

Out[4]: 
0    POLYGON ((-87.94035 41.93909, -87.93965 41.939...
1    POLYGON ((-87.88849 42.01734, -87.88676 42.016...
2    POLYGON ((-87.92825 42.02102, -87.92652 42.020...
3    POLYGON ((-87.86428 42.04548, -87.86255 42.045...
4    POLYGON ((-87.86947 42.05987, -87.86774 42.059...
5    POLYGON ((-87.87466 42.08422, -87.87293 42.084...
6    POLYGON ((-88.03025 42.10923, -88.02852 42.109...
7    POLYGON ((-88.01296 42.10972, -88.01123 42.109...
8    POLYGON ((-87.90750 42.12355, -87.90577 42.123...
9    POLYGON ((-88.01296 42.13131, -88.01123 42.130...
Name: geometry, dtype: geometry

最后出现的是这两个数字。它们具有相同的轴,我只是想让它们在一个图形上对齐(红点在两个图形中的相同位置,但我知道我遗漏了什么。

我试过将几何对象的轴设置为与地理图像相同,将它们变成同一图形的子图,切换 zorder 并将它们放在同一图形中,等等.但似乎没有任何效果。我在绘图方面没有太多经验,因此将不胜感激。

我将您的代码行重新排列到适当的位置,added/corrected 必要时投影数据。不相关的行被注释掉了。最后,这是您可以尝试的结果代码。

from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.io.img_tiles as cimgt
import geopandas as gpd   
from matplotlib import pyplot as plt  
import cartopy.crs as ccrs

stamen_terrain = cimgt.Stamen('terrain-background')
fig, ax = plt.subplots(figsize=(8,6), subplot_kw={'projection': stamen_terrain.crs})

ax.set_extent([-89, -86, 41, 43], crs=ccrs.PlateCarree())

# Plotting the trade area graph
#trade_geo_df = trade_area_response.json()['data']
#trade_geo_df = gpd.GeoDataFrame(trade_geo_df)
#trade_geo_df.loc[:, 'coordinates'] = trade_geo_df.coordinates.map(lambda x: x[0])
#trade_geo_df['geometry'] = trade_geo_df.coordinates.apply(shapely.geometry.Polygon)
#trade_geo_df['geometry'].plot(ax=ax)   
ax.plot(-87, 42, markersize=20, marker='o', color='red', transform=ccrs.PlateCarree(), zorder=100)  # Just a red dot

gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=2, color='gray', alpha=0.5, linestyle='--')
gl.ylabels_right = False
gl.xlabels_top = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 10, 'color': 'gray'}
gl.ylabel_style = {'size': 10, 'color': 'gray'}

ax.add_image(stamen_terrain, 8)
#ax.plot(placer_lng, placer_lat, markersize=2, marker='o', color='red', transform=ccrs.Geodetic())  # Just another red dot
plt.show()

输出:-