Geopandas 为绘图上的点添加标签

J-m*_*man 8 python-3.x geopandas

我有一个带有几何列和带有点名称的列的地理数据框“all_locations”。在地图上绘制点效果很好,但我想用位置名称注释点。


['位置'] ['几何']
BUITHVN8 POINT()

(当然,实际数据框要大得多)

我试过这个(和其他事情):

    all_locations['coords'] = all_locations['geometry'].apply(lambda x: x.point.coords[:])
all_locations['coords'] = [coords[0] for coords in all_locations['coords']]

all_locations.plot(ax=ax)
for idx, row in all_locations.iterrows():
    plt.annotate(s=row['locatie'], xy=row['geometry'])
Run Code Online (Sandbox Code Playgroud)

添加坐标列但它给出了这个错误:“点”对象没有属性“点”

jor*_*ris 18

使用citiesgeopandas 中包含的示例数据集,您可以执行以下操作:

import geopandas
cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))

ax = cities.plot()

for x, y, label in zip(cities.geometry.x, cities.geometry.y, cities.name):
    ax.annotate(label, xy=(x, y), xytext=(3, 3), textcoords="offset points")
Run Code Online (Sandbox Code Playgroud)

  • 只是对这种循环方法与应用方法感到好奇。`points_clip.apply(lambda x: `ax.annotate(s=x['name'], xy=x.geometry.coords[0], xytext=(3, 3), textcoords="偏移点"), axis =1);`有什么想法吗?apply 似乎效果很好。 (2认同)