【问题标题】:Geopandas add labels to points on plotGeopandas 为绘图上的点添加标签
【发布时间】:2018-10-20 14:04:49
【问题描述】:

我有一个地理数据框“all_locations”,其中包含一个几何列和一个包含点名称的列。在地图上绘制点可以正常工作,但我想用位置名称注释这些点。


['位置'] ['几何']
BUITHVN8 点()

(实际数据框当然要大得多)

我已经尝试过这个(和其他东西):

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

all_locations.plot(ax=ax)
for idx, row in all_locations.iterrows():
    plt.annotate(s=row['locatie'], xy=row['geometry'])

添加坐标列但出现此错误:''Point' object has no attribute 'point'

【问题讨论】:

    标签: python-3.x geopandas


    【解决方案1】:

    使用 geopandas 中包含的 cities 示例数据集,您可以按以下方式执行此操作:

    import geopandas
    cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))
    
    ax = cities.plot()
    
    for x, y, label in zip(cities.geometry.x, cities.geometry.y, cities.name):
        ax.annotate(label, xy=(x, y), xytext=(3, 3), textcoords="offset points")
    

    【讨论】:

    • 只是对这种循环方法与应用感到好奇。 points_clip.apply(lambda x: ax.annotate(s=x['name'], xy=x.geometry.coords[0], xytext=(3, 3), textcoords="offset points"), axis=1);` 任意想法? apply 似乎工作得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 2021-01-11
    • 1970-01-01
    相关资源
    最近更新 更多