matplotlib GeoPandas标注面

ogsagwnx  于 2022-11-24  发布在  其他
关注(0)|答案(2)|浏览(183)

给定可用的形文件here:我想在Map上标记每个多边形(县)。这可以用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()

提前感谢!

mspsb9vt

mspsb9vt1#

c['geometry']是由shapely.geometry.polygon.Polygon对象组成的序列。您可以通过检查

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

Shapely docs中有一个方法representative_point(),它
返回保证位于几何对象内的低成本计算点。
听起来非常适合需要标注多边形对象的情况!然后可以为geopandasdataframe'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')

n9vozmp4

n9vozmp42#

不需要循环,下面是使用apply进行注解的方法:

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

相关问题