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

散点图移动标签

  •  0
  • Jellyse  · 技术社区  · 8 年前

    我创建了一些大型港口的地图。带有“x”和“y”纬度和经度以及“text”端口名。

    x,y = map(lonA, latA)
    map.scatter(x, y, s=Size, c=color, marker='o', label = 'Ports',alpha=0.65, zorder=2)
    for i in range (0,n):
      plt.annotate(text[i],xy=(x[i],y[i]),ha='right')
    

    我绘制的点(较大的点表示较大的端口)与标签重叠。如何将它们绘制得更远一些,以提高可读性? enter image description here

    2 回复  |  直到 8 年前
        1
  •  1
  •   Zsolt Diveki    8 年前

    您可以使用 xytext 用于调整文本位置的参数:

    plt.annotate(text[i],xy=(x[i],y[i]),xytext=(x[i]+10,y[i]+10), ha='right')
    

    在这里,我为您的xy位置添加了10。 有关更多信息,请查看以下建议: https://matplotlib.org/users/annotations_intro.html

        2
  •  0
  •   Jellyse    8 年前

    @KiralySandor你是对的,但你需要将文本坐标更改为数据。

    for i in range (0,n):
      plt.annotate(text[i],xy=(x[i],y[i]),textcoords='data',xytext=(x[i]-9000,y[i]),ha='right')
    

    enter image description here

    现在,名字更偏左了。