Geopandas add labels to points on plot

那年仲夏 提交于 2020-08-01 16:57:32

问题


I have a geodataframe 'all_locations' with a geometry column and a column with the name of the point. Plotting the points on a map works just fine but I want to annotate the points with location name.


['location'] ['geometry']
BUITHVN8 POINT()

(Actual dataframe is much larger of course)

I have tried this (and other things):

    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'])

Adding a coordinates column but it gives this error: ''Point' object has no attribute 'point'


回答1:


Using the cities example dataset included in geopandas, you can do this as follows:

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")


来源:https://stackoverflow.com/questions/50270565/geopandas-add-labels-to-points-on-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!