简体   繁体   中英

Geopandas add labels to points on plot

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM