2

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?

DevB2F
  • 4,674
  • 4
  • 36
  • 60

1 Answers1

4

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

Wes Doyle
  • 2,199
  • 3
  • 17
  • 32