目前,我有一个来自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
并将它们放置在同一图形中,等等。但似乎没有任何效果。我没有太多的绘图经验,因此非常感谢任何帮助
我将代码行重新排列到适当的位置,在必要时添加/更正投影数据。不相关的行被注释掉。最后,这里是您可以尝试的结果代码
输出:-
相关问题 更多 >
编程相关推荐