地理 pandas 为情节上的点添加标签 [英] Geopandas add labels to points on plot

查看:155
本文介绍了地理 pandas 为情节上的点添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个geodataframe'all_locations',其中有一个几何列和一个带有点名称的列.在地图上绘制点可以很好地工作,但是我想用位置名称注释这些点.

I have a geodataframe 'all_locations' with a geometry column and a column with the name of the point. Plotting the points on a map works just fine but I want to annotate the points with location name.

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

['location'] ['geometry']
BUITHVN8 POINT()

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

(Actual dataframe is much larger of course)

我已经尝试过此操作(和其他操作):

I have tried this (and other things):

    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'])

添加坐标列,但会出现此错误:点"对象没有属性点"

Adding a coordinates column but it gives this error: ''Point' object has no attribute 'point'

推荐答案

使用Geopandas中包含的cities示例数据集,您可以按照以下步骤进行操作:

Using the cities example dataset included in geopandas, you can do this as follows:

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")

这篇关于地理 pandas 为情节上的点添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆