GeoPandas 标签多边形
GeoPandas Label Polygons
鉴于可用的形状文件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()
提前致谢!
c['geometry']
是由 shapely.geometry.polygon.Polygon
个对象组成的系列。您可以通过检查
来验证这一点
In [23]: type(c.ix[23, 'geometry'])
Out[23]: shapely.geometry.polygon.Polygon
从 Shapely docs there is a method representative_point()
那
Returns a cheaply computed point that is guaranteed to be within the
geometric object.
听起来非常适合需要标记多边形对象的情况!然后,您可以像这样 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')
无需循环,下面是如何使用应用进行注释:
ax = df.plot()
df.apply(lambda x: ax.annotate(text=x['NAME'], xy=x.geometry.centroid.coords[0], ha='center'), axis=1);
鉴于可用的形状文件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()
提前致谢!
c['geometry']
是由 shapely.geometry.polygon.Polygon
个对象组成的系列。您可以通过检查
In [23]: type(c.ix[23, 'geometry'])
Out[23]: shapely.geometry.polygon.Polygon
从 Shapely docs there is a method representative_point()
那
Returns a cheaply computed point that is guaranteed to be within the geometric object.
听起来非常适合需要标记多边形对象的情况!然后,您可以像这样 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')
无需循环,下面是如何使用应用进行注释:
ax = df.plot()
df.apply(lambda x: ax.annotate(text=x['NAME'], xy=x.geometry.centroid.coords[0], ha='center'), axis=1);