4

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

vendgreen
  • 103
  • 1
  • 1
  • 5
  • Does this answer your question? [xytext details in Matplotlibs Annotate](https://stackoverflow.com/questions/44928011/xytext-details-in-matplotlibs-annotate) – JohanC Mar 21 '20 at 10:39

1 Answers1

8

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

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • You're welcome. If your problem is solved, consider accepting the answer using the checkmark on the left to close the topic – Diziet Asahi Mar 21 '20 at 10:36
  • 1
    When I try to use "offset points" with text annotations I'm getting this error: ValueError: xycoords cannot be an offset coordinate – Veggiet May 07 '21 at 06:13