pythonmatplotlibannotationsfigure

Python plot with annotating arrows


I have a python script that plot temperatures. Since some stations are too close, it causes overlap. I am trying to use annotating arrows to make the values more visible. Here is an example of a zommed map

enter image description here

How can I adjust the arrows so that the values stay near the edge (not besides the mark)?

Here's a code snippet

# Labels.
sc = plt.scatter(lon, lat, c=temp, s=20, cmap="RdYlGn_r")
for label, xpt, ypt in zip(temp, lon, lat):
    plt.text(xpt + 0.02, ypt, label, ha='left', va='center', color='black')  

    # arrow to the label
    plt.arrow(xpt, ypt, 0.15, 0.15, head_width=0.02, head_length=0.02, fc='black', ec='black')

# add colorbar
cbar = plt.colorbar(sc)

# pic layout adjust
plt.tight_layout()


plt.show()

Solution

  • Your arrow starts at (xpt, ypt) and finishes at (xpt + 0.15, ypt + 0.15).

    In order to put the label near the edge, you can offset your text object as such:

    plt.text(xpt + dx + 0.02, ypt + dy, label, ha='left', va='center', color='black') 
    

    where dx=0.15 and dy=0.15 in your case.