问题
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