GeoPandas Label Polygons

Given the available form file here : I would like to mark each polygon (county) on the map. Is this possible with GeoPandas?

import geopandas as gpd import matplotlib.pyplot as plt %matplotlib inline shpfile=<Path to unzipped .shp file referenced and linked above> c=gpd.read_file(shpfile) c=c.loc[c['GEOID'].isin(['26161','26093','26049','26091','26075','26125','26163','26099','26115','26065'])] c.plot() 

Thanks in advance!

+9
source share
3 answers

c['geometry'] is a series consisting of shapely.geometry.polygon.Polygon objects. You can verify this by setting

 In [23]: type(c.ix[23, 'geometry']) Out[23]: shapely.geometry.polygon.Polygon 

From Shapely docs there is a representative_point() method that

Returns an inexpensive computed point that is guaranteed to be within a geometric object.

Sounds perfect for situations where you need to label polygon objects! Then you can create a new column for geopandas dataframe , 'coords' like this

 c['coords'] = c['geometry'].apply(lambda x: x.representative_point().coords[:]) c['coords'] = [coords[0] for coords in c['coords']] 

Now that you have a set of coordinates related to each object of the polygon (each district), you can annotate your plot, iterate through your dataframe

 c.plot() for idx, row in c.iterrows(): plt.annotate(s=row['NAME'], xy=row['coords'], horizontalalignment='center') 

enter image description here

+25
source

no need for a loop, here's how you can comment with:

 ax = df.plot() df.apply(lambda x: ax.annotate(s=x.NAME, xy=x.geometry.centroid.coords[0], ha='center'),axis=1); 
+15
source

a very valuable share ... When I tried the tozCSS code, I got individual cards marked as an attachment. How can I fix this. Can you help me???

enter image description here

0
source

All Articles