代码之家  ›  专栏  ›  技术社区  ›  LucieCBurgess

Matplotlib映射和子块,每个数据点有不同的文本

  •  0
  • LucieCBurgess  · 技术社区  · 7 年前

    我正在绘制一些数据,伦敦各地的气象站,并想用气象站的名称标记每个数据点(绿色点),例如Bow,Westminster。
    地图是从Geopandas数据框中的geometry列创建的。使用此帖子: https://stackoverflow.com/questions/14432557/matplotlib-scatter-plot-with-different-text-at-each-data-point 我试图对文本进行批注,但出现以下错误:
    TypeError: 'Point' object is not iterable .

    这是目前的地图:

    Weather station data - green points

    下面是代码:

    f, ax = plt.subplots(1, figsize=(12, 12))
    ax.set_title('Plot of London Grid for Learning weather stations with London boroughs and locations of Urban Mind datapoints', fontsize = 15)
    
    boroughs.plot(alpha=1,edgecolor='w', facecolor='gray', linewidth=0.1, ax=ax)
    london_umdata_geo.plot(ax=ax, marker="o",color="red",markersize=10.0,alpha=1.0, legend=True) #1,015 UM datapoints in London
    stations_geo.plot(ax=ax, marker="X",color='green',markersize=20.0,alpha=1.0,legend=True)
    
    
    n = stations_geo['Station Name'].tolist()
    for i, txt in enumerate(n):
        ax.annotate(txt, xy = stations_geo.loc[i,'geometry'])
    
    plt.show()
    

    站点为绿色点,其他数据点(不带标签)为红色。

    stations_geo 是GeoPandas数据帧,其中包含 geometry 使用matplotlib从中打印点的列。

    我对annotate方法使用一个循环,据说循环遍历geometry列中的点,并将这些点用作注释的坐标,但是matplotlib似乎不喜欢点对象(它们是形状点)。但是,这些点直接被用来创建绿色点,所以我不确定哪里出错了。

    感谢您的帮助,谢谢!

    1 回复  |  直到 7 年前
        1
  •  1
  •   ImportanceOfBeingErnest    7 年前

    你自己已经发现了问题( “matplotlib似乎不喜欢点对象(它们是形状点)” ).

    因此,当遇到这样的问题时,您可以使用以下文档。

    1. 看看 matplotlib documentation 关于你可以提供给 xy 论证 annotate .

      xy型 :iterable公司
      长度2指定要注释的(x,y)点的序列

    2. 看看你的东西。出于怀疑,使用 print(type(stations_geo.loc[i,'geometry'])) . 它会显示出 shapely.geometry.Point . 看看 the documentation of that object .

    3. 确定将一个转换为另一个的方法。在这种情况下你会发现

      坐标值通过 coords , x , y ,和 z 财产。

    4. 打印 stations_geo.loc[i,'geometry'].coords ,它将是(单个)元组的列表。 所以为了把一个 Point 对于python元组,可以使用 .coords 属性并从该列表中获取第一个(也是唯一的)元素。

      xy = stations_geo.loc[i,'geometry'].coords[0]