繁体   English   中英

GeoPandas 标签多边形

[英]GeoPandas Label Polygons

鉴于此处可用的形状文件:我想标记地图中的每个多边形(县)。 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()

提前致谢!

c['geometry']是由shapely.geometry.polygon.Polygon对象组成的系列。 您可以通过检查来验证这一点

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

Shapely docs有一个方法representative_point()

返回保证在几何对象内的廉价计算点。

听起来非常适合需要标记多边形对象的情况! 然后,您可以为您的geopandas dataframe geopandas创建一个新列,像这样'coords'

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

现在您有一组与每个多边形对象(每个县)有关的坐标,您可以通过遍历数据框来注释您的绘图

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

在此处输入图片说明

无需循环,以下是使用 apply 进行注释的方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM