How do you offset text in a scatter plot in matplotlib?

Viewed 8100

I've annotated two specific points in my PCA but the text is in the middle of a bunch of points and hard to read. I would like to move it down (and add arrows which I think I've already done succesfully). Can anyone help?

I've made the text in the following way:

for i, txt in enumerate(cluster_center_names):
    plt.annotate(txt,(x_cluster_center[i],y_cluster_center[i]), weight="bold", fontsize=10, arrowprops=dict(arrowstyle="->", color='black'))

enter image description here

1 Answers

Use xytext=(x,y) to set the coordinates of the text. You can provide these coordinates in absolute values (in data, axes, or figure coordinates), or in relative position using textcoords="offset points" for example.

More example at the annotation tutorial

x1,y1 = 0,0
x2,y2 = 20,50
fig, ax = plt.subplots()
ax.scatter(x1,y1)
ax.annotate("Annotation",
            xy=(x1, y1), xycoords='data',
            xytext=(x2, y2), textcoords='offset points',
            arrowprops=dict(arrowstyle="->", color='black')
            )

enter image description here

Related