matplotlib .xlabel() beheben: AttributeError: 'AxesSubplot' object has no attribute 'xlabel'

English Deutsch

Problem:

Die xlabel-Beschriftung eines matplotlib-Plots soll mit .xlabel("Meine xlabel-Beschriftung") gesetzt werden, aber es wird eine Fehlermeldung wie folgende angezeigt

matplotlib_xlabel_error.txt
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-28a87d95bccc> in <module>
---> 11 axs[0].xlabel("My xlabel")

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

Lösung

.set_xlabel("Meine xlabel-Beschriftung") anstelle von .xlabel("Meine xlabel-Beschriftung") verwenden!

Während

matplotlib_plt_xlabel_example.py
from matplotlib import pyplot as plt

plt.xlabel("My xlabel")

funktioniert, muss bei einem Axes-Objekt wie dem von plt.subplots() zurückgegebenen set_xlabel() verwendet werden!

matplotlib_axes_set_xlabel_example.py
from matplotlib import pyplot as plt
fig, axs = plt.subplots(2, 1)
# ...
axs[0].set_xlabel("My xlabel")

Check out similar posts by category: Python