Search code examples
pythonmatplotlibaxis-labelsyaxis

Rotating the y-axis label


I am trying to rotate the title of the Y axis so it is horizontal. I do not want the tick labels horizontal just the title of the Y axis. I have to use subplots as I am making multiple plots at once. Here is the below script in which I have tried to rotate the Y axis title.

import matplotlib.pyplot as plt
import sys

fig, ax = plt.subplots()

ax.set_title(r'$\alpha$ > \beta_i$', fontsize=20)
ax.set(xlabel='meters $10^1$', ylabel=r'Hertz $(\frac{1}{s})$')
ax.set(xlabel=r's(t) = \mathcal(A)\/\sin(2 \omega t)', ylabel=r'Hertz $(\frac{1}{s})$')
ax.set(ylabel="North $\uparrow$",fontsize=9,rotate=90)
plt.show()

When I run it I get an error:

TypeError: There is no AxesSubplot property "rotate"

How can I tweak this program so that the Y axis is rotating horizontally?


Solution

  • By using ax.set you are attempting to set properties of the axes rather than properties of the ylabel text object.

    Rather than using ax.set you can instead use xlabel and ylabel to create the x and y labels and pass in kwargs to modify their appearance. Also the property name is rotation rather than rotate. Also you'll want to set the rotation to 0 as the default is 90 which is why it's rotated in the first place.

    plt.title(r'$\alpha > \beta_i$', fontsize=20)
    plt.xlabel(r'meters $10^1$', fontsize=9)
    plt.ylabel("North $\uparrow$", fontsize=9, rotation=0)
    

    enter image description here