Align text or annotation to plot in python 3.8

967 views Asked by At

I am trying to align some custom text labels to lines in a plot in such a way that rescaling the axes or plot window does not misalign them using python 3.8. The plot is also in log log scale.

The answer I have found over here Text Rotation Relative To Line from matplotlib.org suggests the following:

import matplotlib.pyplot as plt
import numpy as np

# Plot diagonal line (45 degrees)
h = plt.plot(np.arange(0, 10), np.arange(0, 10))

# set limits so that it no longer looks on screen to be 45 degrees
plt.xlim([-10, 20])

# Locations to plot text
l1 = np.array((1, 1))
l2 = np.array((5, 5))

# Rotate angle
angle = 45
trans_angle = plt.gca().transData.transform_angles(np.array((45,)),
                                                   l2.reshape((1, 2)))[0]

# Plot text
th1 = plt.text(l1[0], l1[1], 'text not rotated correctly', fontsize=16,
               rotation=angle, rotation_mode='anchor')
th2 = plt.text(l2[0], l2[1], 'text rotated correctly', fontsize=16,
               rotation=trans_angle, rotation_mode='anchor')

plt.show()

This code taken directly from the very documentation of matplotlib returns the error

AttributeError: 'Text' object has no property 'transform_rotates_text'

Is this a simple issue of using a newer or older version of python? I need this to work in version 3.8. Thanks in advance!

0

There are 0 answers