0

folks. Does anyone know how to rotate the title text for several subplots like this:

Rotate image titles or annotations.

A slight difference between the above images and my desired images is that, each face image in my whole figure was displayed by an individual subplot. And I want to add a rotated title for each subplot figure.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

3

A minimal working answer for you. I hope it helps.

import numpy as np
import matplotlib.pyplot as plt
f, (ax1, ax2) = plt.subplots(1, 2, sharey=False, figsize=(10,4))

x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x)

ax1.plot(x, y)
ax1.set_title('Title 1', y=1.1, rotation = 45) # You can vary y and angle as per choice
ax2.plot(x, 1.5*y)
ax2.set_title('Title 2', y=1.1, rotation = 45)

Output

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Thank you for your kind help! Your method completely solved my problem.:p – Zengjie Song Aug 28 '18 at 03:40
  • If you, like me, also want the rotated title to be better aligned, use `ax1.set_title('Title 1', y=1.1, rotation=45, ha='left', va='baseline')`. This mainly occurs when the title to rotate is a long string. (Suggestion based on an answer to a similar question: https://stackoverflow.com/a/65400381/5433896.) – Sander Vanden Hautte Nov 16 '22 at 10:57