DevB2F
DevB2F

Reputation: 5075

add labels to polygons

I have seen this question where labels are added to polygons and they appear inside each polygon. I am trying to accomplish the same thing, but I am using a different format and can´t see how to apply the same method to my case.

My polygons are like this:

import geopandas as gpd
from shapely.geometry import Polygon
boundary = gpd.GeoSeries({
    'foo': Polygon([(5, 5), (5, 13), (13, 13), (13, 5)]),
    'bar': Polygon([(20, 20), (40, 20), (40, 30), (20, 30)]),
})


boundary.plot(cmap="Greens")
plt.show()

Any idea how to make each polygon have a label?

Upvotes: 2

Views: 3560

Answers (1)

Wes Doyle
Wes Doyle

Reputation: 2287

One approach might be to use the centroids from your polygons and annotate:

ax = boundary.plot(cmap="Greens")

for i, geo in boundary.centroid.iteritems():
    ax.annotate(s=i, xy=[geo.x, geo.y], color="red")

    # show the subplot
    ax.figure

plt.show()

enter image description here

Upvotes: 4

Related Questions