5

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

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome. If your problem is solved, consider accepting the answer using the checkmark on the left to close the topic
When I try to use "offset points" with text annotations I'm getting this error: ValueError: xycoords cannot be an offset coordinate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.