给定可用的形状文件,here:我想标记地图中的每个多边形(县)。使用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()提前感谢!
发布于 2016-08-12 01:17:44
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创建一个新列,'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')

发布于 2017-02-14 05:48:18
不需要循环,以下是如何使用apply进行注释:
ax = df.plot()
df.apply(lambda x: ax.annotate(text=x['NAME'], xy=x.geometry.centroid.coords[0], ha='center'), axis=1);https://stackoverflow.com/questions/38899190
复制相似问题