How to fix matplotlib .ylabel() AttributeError: 'AxesSubplot' object has no attribute 'ylabel'
Problem:
You want to set the ylabel of a matplotlib plot using .ylabel("My ylabel")
but you see an error messsage like
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-28a87d95bccc> in <module>
---> 11 axs[0].ylabel("My ylabel")
AttributeError: 'AxesSubplot' object has no attribute 'ylabel'
Solution:
Use .set_ylabel("My ylabel")
instead of .ylabel("My ylabel")
!
While
from matplotlib import pyplot as plt
plt.ylabel("My ylabel")
works fine, if you have an axes object like the one you get from plt.subplots()
, you’ll have to use set_ylabel()
!
from matplotlib import pyplot as plt
fig, axs = plt.subplots(2, 1)
# ...
axs[0].set_ylabel("My ylabel")