我正在尝试使用 Geopandas 注释 map 上的一些点,但我的问题与 matplotlib 更多相关。
我为 xy 创建了一列坐标点,但在尝试多种方法时收到以下警告。
ValueError: ('not enough values to unpack (expected 2, got 1)', 'occurred at index 0')
我想要注释的文本是id号(pod_id_location),我只是想将这些数字绘制在它们各自的点旁边(或上面),这样我就可以在 map 上绘制时看到哪个id对应于哪个点。我正在尝试使用 lambda apply 将这些值从一列映射到另一列中的坐标。我看不出哪里出了问题,并且在使用 for 循环时遇到相同的错误。感谢您的指点,我的数据和代码如下:
pod_id_location geometry Zone
0 1245 POINT (-22993.152 6728924.970) non-ULEZ
7940 2245 POINT (-13406.652 6714132.654) ULEZ
13934 3245 POINT (-16958.244 6720147.066) non-ULEZ
25068 5245 POINT (-20791.698 6699568.057) non-ULEZ
32551 6245 POINT (-40049.413 6704610.464) non-ULEZ
... ... ... ...
849223 742451 POINT (-12923.302 6720129.538) non-ULEZ
849547 832450 POINT (-6445.287 6717978.299) non-ULEZ
851190 872450 POINT (-4064.943 6697346.893) non-ULEZ
852852 892450 POINT (6744.514 6713063.033) non-ULEZ
853523 982450 POINT (-958.349 6706975.535) non-ULEZ
ax = gdf_2[gdf_2['Zone'] == 'ULEZ'].plot(figsize=(10, 8))
gdf_2.apply(lambda x: ax.annotate(s=x.pod_id_location, xy=x.geometry), axis=1)
plt.axis('off')
plt.show()
最佳答案
我通过将 lambda 函数更改为以下内容成功解决了该问题:
gdf_2.apply(lambda x: ax.annotate(s=x.pod_id_location, xy=x.loc['geometry'].coords[0]), axis=1)
因为 shapley 对象的第一个元素是坐标。由于某种原因,我的情节现在打印很小......但这就是我要解决的问题......
关于python - 在 matplotlib 和 geopandas 中注释 xy 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60469719/