Annotating xy points in matplotib and geopandas

Viewed 966

I am trying to annotate some points on a map using Geopandas but my question is more related to matplotlib.

I have created a column of coordinate points for my xy but get the following warning when trying a number of approaches.

ValueError: ('not enough values to unpack (expected 2, got 1)', 'occurred at index 0')

enter image description here

The text I want to annotate are id numbers (pod_id_location), I simply want to plot these numbers next to (or on) their respective points, just so that I can see which id's correspond to which point when plotting on a map. I am trying to use lambda apply to map these values from one column to the coordinates in another column. I can't see where I am going wrong, and get the same error is using a for loop. Thanks for any pointers, my data and code is below:

    pod_id_location geometry    Zone
0   1245    POINT (-22993.152 6728924.970)  non-ULEZ
7940    2245    POINT (-13406.652 6714132.654)  ULEZ
13934   3245    POINT (-16958.244 6720147.066)  non-ULEZ
25068   5245    POINT (-20791.698 6699568.057)  non-ULEZ
32551   6245    POINT (-40049.413 6704610.464)  non-ULEZ
... ... ... ...
849223  742451  POINT (-12923.302 6720129.538)  non-ULEZ
849547  832450  POINT (-6445.287 6717978.299)   non-ULEZ
851190  872450  POINT (-4064.943 6697346.893)   non-ULEZ
852852  892450  POINT (6744.514 6713063.033)    non-ULEZ
853523  982450  POINT (-958.349 6706975.535)    non-ULEZ
ax = gdf_2[gdf_2['Zone'] == 'ULEZ'].plot(figsize=(10, 8))

gdf_2.apply(lambda x: ax.annotate(s=x.pod_id_location, xy=x.geometry), axis=1)

plt.axis('off')
plt.show()
1 Answers

I have managed to solve the issue by changing my lambda function to this:

gdf_2.apply(lambda x: ax.annotate(s=x.pod_id_location, xy=x.loc['geometry'].coords[0]), axis=1)

Since the first element of a shapley object are the coordinates. For some reason my plot now prints tiny... but thats something for me to work out...

Related