'Text' object has no property 'loc' error in matplotlib

Viewed 34

I made code like below.

import matplotlib.pyplot as plt

x=[1, 2, 3]
y = [2, 4, 8]
plt.plot(x, y)
plt.xlabel('X', color='red', fontdict={'size':15}, loc='right') 
plt.ylabel('Y', color='green', fontdict={'size':15}, loc='top') 

But I got error like this. How can I fix this code?

enter image description here

1 Answers

Your code works for me.

Maybe check your matplotlib version and your python version.

I have these

pip show matplotlib


Name: matplotlib
Version: 3.5.1
python --version
Python 3.9.10
Related