0

I created a map of some large ports. With 'x' and 'y' latitude and longitude and 'text' the port names.

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

The dots I plotted (bigger dots for bigger ports) overlap with the labels. How do I plot them a little further away to increase readability? enter image description here

Jellyse
  • 839
  • 7
  • 22

2 Answers2

1

You can use the xytext parameter to adjust the text position:

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

Here I added 10 to your xy position. For more you can look up the suggestions here: https://matplotlib.org/users/annotations_intro.html

Zsolt Diveki
  • 159
  • 8
  • I tried it but doesn't work. :( Even if I do +100 (xytext=(x[i]+100,y[i]+100)) there's no difference... – Jellyse Mar 14 '18 at 10:49
  • 1
    Is it possible that your `x` and `y` axes cover a huge range, like millions, where a 100 movement would be small? – Zsolt Diveki Mar 14 '18 at 10:58
  • No normally not. My axes are in degrees latitude and longitude, so +10 would be +10 degrees. Normally, they should be moving very far, but they don't move at all. – Jellyse Mar 14 '18 at 11:08
  • I do not know what object is `map` in your script, but you are using a `scatter` method on it. Is it possible to use the `annotate` method on it too? `map.annotate` instead of `plt.annotate`? – Zsolt Diveki Mar 14 '18 at 11:16
  • It's just the name. If I do map.annotate, it doesn't print any labels – Jellyse Mar 14 '18 at 11:58
  • you were right I did - 9000 and it worked. 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') – Jellyse Mar 14 '18 at 12:08
0

@KiralySandor you were right, but you need to change textcoordinates to data.

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

Now the names are slighlty more to the left.

Jellyse
  • 839
  • 7
  • 22