与世界地图一起绘制坐标

问题描述 投票:0回答:1

我有这个代码:

import geopandas as gpd
import pandas as pd

from shapely.geometry import Point

cities = pd.read_excel("C:/....xlsx")

city_geometry = [Point(xy) for xy in zip(cities['Longitude'], cities['Latitude'])]
city_geodata = gpd.GeoDataFrame(cities, geometry=city_geometry)
city_geodata = city_geodata.set_crs(epsg=27700, allow_override=True)

gdf =gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

fig, ax = plt.subplots(figsize=(20,20))
city_geodata.plot(ax=ax)
gdf.plot(ax=ax)
plt.show()

我希望在世界地图上有我的城市点。但它只绘制城市点。我的错误在哪里?

python matplotlib geopandas
1个回答
0
投票

您的代码几乎是正确的,但是您需要进行一些小调整以确保城市点和世界地图都绘制在同一轴上。您应该首先绘制世界地图,然后在其上绘制城市点。修改您的代码如下:

import geopandas as gpd
import pandas as pd
from shapely.geometry import Point

cities = pd.read_excel("C:/....xlsx")

city_geometry = [Point(xy) for xy in zip(cities['Longitude'], cities['Latitude'])]
city_geodata = gpd.GeoDataFrame(cities, geometry=city_geometry)
city_geodata = city_geodata.set_crs(epsg=4326, allow_override=True)

gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

fig, ax = plt.subplots(figsize=(20, 20))
gdf.plot(ax=ax)
city_geodata.plot(ax=ax, color='red', markersize=20)
plt.show()

在这段代码中,我将

city_geodata
的CRS更改为EPSG:4326,与世界地图CRS相同。然后,我首先绘制了世界地图(
gdf.plot(ax=ax)
),然后在其上绘制了城市点(
city_geodata.plot(ax=ax, color='red', markersize=20)
)。这应该按预期在世界地图上显示城市点。

© www.soinside.com 2019 - 2024. All rights reserved.