Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 658
J-man Avatar asked May 10 '18 10:05

J-man


1 Answers

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")
like image 174
joris Avatar answered Sep 19 '22 08:09

joris