Python 注释城市名称
我想在坐标Python 注释城市名称,python,matplotlib,geopandas,contextily,Python,Matplotlib,Geopandas,Contextily,我想在坐标xy=(52.52,13.405)处注释柏林的城市名称。我尝试了ax.annotate(),这会产生一个奇怪的映射。也许这和坐标的CRS有关 import geopandas as gpd import contextily as ctx world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) world = world[(world.name=="Germany")] world = world.to_
xy=(52.52,13.405)
处注释柏林的城市名称。我尝试了ax.annotate()
,这会产生一个奇怪的映射。也许这和坐标的CRS有关
import geopandas as gpd
import contextily as ctx
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[(world.name=="Germany")]
world = world.to_crs(epsg=3857)
ax = world.plot(figsize=(10, 10), color='none', linewidth=1, alpha=0.5)
ax.annotate("Berlin", xy=(52.52, 13.405))
ctx.add_basemap(ax, url=ctx.providers.Stamen.Watercolor, zoom=9)
根据您的代码,应该如下所示:
ax.annotate("Berlin", xy=(52.52, 13.405))
我最初的代码有两个缺陷。正如其他答案所指出的,我错误地使用了ax.annoate() 此外,
world
被转换为espg=3857
。柏林市的坐标不是。通过变换坐标,它可以工作:
import geopandas as gpd
import contextily as ctx
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[(world.name=="Germany")]
world = world.to_crs(epsg=3857)
ax = world.plot(figsize=(10, 10), color='none', linewidth=1, alpha=0.5)
ax.annotate("Berlin", xy=(1491636.9565986055, 6895388.533179172))
ctx.add_basemap(ax, url=ctx.providers.Stamen.Watercolor, zoom=9)
不熟悉
geopandas
但在matplotlib.pyplot
中,annotate
的语法是ax.annotate('text',(x,y))
。我猜geopandas
是在matplotlib
上写的。试试这个ax。注释('Berlin',xy)
不起作用。也许这和坐标的CRS有关?