Rotating the y-axis label

Question:

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?

Asked By: jms1980

||

Answers:

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

Answered By: Suever

While the other answer works, it requires switching from the explicit Axes interface, to the implicit pyplot interface, which shouldn’t be done.

set_ylabel()

**kwargs Text properties: Text properties control the appearance of the label.

matplotlib.text

position Set the (x, y) position of the text. xy: (float, float)

rotation Set the rotation of the text. s: float or {'vertical', 'horizontal'}. The rotation angle in degrees in mathematically positive direction (counterclockwise). ‘horizontal’ equals 0, ‘vertical’ equals 90.

rotation_mode Set text rotation mode. m: {None, 'default', 'anchor'}. If None or 'default', the text will be first rotated, then aligned according to their horizontal and vertical alignments. If 'anchor', then alignment occurs before rotation.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.set_title(r'$alpha > beta_i$', fontsize=20)
ax.set(xlabel='meters $10^1$')
ax.set_ylabel(r'$North uparrow$', fontsize=9, rotation=0)
plt.show()

enter image description here

Answered By: Trenton McKinney