如何将真实地图添加为绘图的背景

2024-10-01 09:31:23 发布

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

我试图在绘图后添加一个真实的地图,以显示数据点的位置

但我不知道如何添加真正的地图

我试过几种方法,比如在情节后面放另一个png

但这是不对的

这是我得到的

import geopandas as gpd
import tilemapbase

BBox = (df.Longitude.min(), df.Longitude.max(), df.Latitude.min(), df.Latitude.max())
print(BBox)
(3.3990449, 7.042425700000001, 50.8134557, 53.4066264)

tilemapbase.start_logging()
tilemapbase.init(create=True)
extent = tilemapbase.extent_from_frame(NL_shape, buffer = 25)

fig, ax = plt.subplots(figsize=(10,10))
plotter = tilemapbase.Plotter(extent, tilemapbase.tiles.build_OSM(), width=1000)
plotter.plot(ax)
ax.set_xlim(BBox[0]-0.2, BBox[1]+0.2)
ax.set_ylim(BBox[2]-0.5, BBox[3]+0.5)
NL_shape.plot(ax=ax, alpha=0.3, edgecolor="black", facecolor="white")
stores_shape.plot(ax=ax, alpha = 0.2, color="red", marker='o');

我按照这里的指示:https://rosenfelder.ai/create-maps-with-python/

我使用geopandas读取国家边界(json),然后绘制数据

但是我不能使用tilemapbase下载realmap

结果如下:

enter image description here

有人知道如何解决这个问题吗?多谢各位

预期的图像如下所示。基本地图应该像OpenStreetmap一样

enter image description here

在我使用上下文后,我出现了一个错误

它显示如下:

enter image description here

enter image description here


Tags: 数据importdfplot地图axminextent
1条回答
网友
1楼 · 发布于 2024-10-01 09:31:23

使用为GeoPandas制作的contextily库。-https://contextily.readthedocs.io/en/latest/

import contextily as cx

fig, ax = plt.subplots(figsize=(10,10))
NL_shape.plot(ax=ax, alpha=0.3, edgecolor="black", facecolor="white")
stores_shape.plot(ax=ax, alpha = 0.2, color="red", marker='o');
cx.add_basemap(ax, crs=NL_shape.crs, source=cx.providers.OpenStreetMap.Mapnik)

相关问题 更多 >