I'm trying to draw a line with text written on it using the following code:
def angle_line(A, B):
x = B[0] - A[0]
y = B[1] - A[1]
angle = math.atan2(y, x )
return angle*180/np.pi
fig, ax = plt.subplots()
xy_init = (0,0)
xy_end = (1, 0.5)
#PLOT LINE
ax.plot((xy_init[0], xy_end[0]), (xy_init[1], xy_end[1]), color='black', linewidth=0.5, linestyle='--')
center = (xy_init[0]+xy_end[0])/2, (xy_init[1]+xy_end[1])/2
angle = angle_line(xy_init, xy_end)
#PLOT TEXT
ax.text(*center, 'TEST', horizontalalignment='center', rotation = angle,
verticalalignment='center',fontdict={'fontsize': 20}, color='red')
Basically, I draw the line using the two points, and then for the text, I calculate the angle and rotate the text accordingly. But, for some reason I cannot comprehend, they are misaligned, as shown in the picture below:
Do I understand something wrong about the parameters of the matplotlib functions, or is there any problem with the code above?