向多边形添加标签

add labels to polygons

其中标签被添加到多边形并且它们出现在每个多边形内。 我正在尝试完成同样的事情,但我使用的是不同的格式,看不到如何将相同的方法应用于我的案例。

我的多边形是这样的:

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()

知道如何让每个多边形都有标签吗?

一种方法可能是使用多边形的质心并注释:

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()