shaare.it

AttributeError: 'Axes' object has no attribute 'xlabel'

AttributeError: ‘Axes’ object has no attribute ‘xlabel’

$ python script.py
Traceback (most recent call last):
  ...
AttributeError: 'AxesSubplot' object has no attribute 'xlabel'

Why this happens

When using the object-oriented interface (working with ax objects), the method names are slightly different from the plt interface. plt.xlabel() becomes ax.set_xlabel().

Fix

Use set_xlabel() instead of xlabel() when working with an Axes object.

Wrong code

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.xlabel("Time")

Fixed code

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_xlabel("Time")