The following code puts some points on the plane and draws a line from center to each point. For each point, there is a label and want to put the label after the point. Therefore, from center, we see a line, then a point and then a text. I want to put the label with the same slope of the line.
Currently, I have this code, but as you can see the rotated text is not properly aligned. How can I fix that?
import matplotlib.pyplot as plt
import numpy as np
from math import *
a = np.array([
[-0.108,0.414],
[0.755,-0.152],
[0.871,-0.039],
],)
labels = ["XXXXXXX", "YYYYYY", "ZZZZZZZ"]
x, y = a.T
plt.scatter(x, y)
plt.xlim(-1,1)
plt.ylim(-1,1)
ax = plt.axes()
for i in range(a.shape[0]):
px = a[i,0]
py = a[i,1]
ax.arrow(0, 0, px, py, head_width=0, head_length=0.1, length_includes_head=True)
angle = degrees(atan(py/px))
ax.annotate(labels[i], (px, py), rotation=angle)
plt.grid(True)
plt.show()
UPDATE:
I used the solution proposed here and modified
text_plot_location = np.array([0.51,0.51])
trans_angle = plt.gca().transData.transform_angles(np.array((45,)),text_plot_location.reshape((1,2)))[0]
ax.annotate(labels[i], (px, py), rotation=text_plot_location)
However, I get this error TypeError: unhashable type: 'numpy.ndarray'