18

I created 3 subplots using subplot(). Now I'd like to add titles for each subplot. Which one of title() and suptitle() shall I use?

In general, what is the difference between them?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
pyan
  • 3,577
  • 4
  • 23
  • 36
  • I think `title` applies to a specific axis within the figure, whereas `suptitle` applies to the whole figure (and all axes within it) – Jake Levi Sep 04 '22 at 12:04

1 Answers1

18

You can set the main figure title with fig.suptitle and subplot's titles with ax.set_title or by passing title to fig.add_subplot. For example:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-np.pi, np.pi, 0.01)

fig = plt.figure()
fig.suptitle('Main figure title')

ax1 = fig.add_subplot(311, title='Subplot 1 title')
ax1.plot(x, np.sin(x))

ax2 = fig.add_subplot(312)
ax2.set_title('Subplot 2 title')
ax2.plot(x, np.cos(x))

ax3 = fig.add_subplot(313)
ax3.set_title('Subplot 3 title')
ax3.plot(x, np.tan(x))

plt.show()

enter image description here

(You may need to manually tweak the font sizes to get the styling you want). I think subtitles need special placement and sizing of the text. For example, see Giving graphs a subtitle in matplotlib

Community
  • 1
  • 1
xnx
  • 24,509
  • 11
  • 70
  • 109
  • 3
    Thanks for the concrete example. I misread `suptitle()` to `subtitle()`. Now it makes sense. – pyan May 07 '15 at 19:19
  • See also the methods [matplotlib.pyplot.title](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.title.html) and [matplotlib.pyplot.suptitle](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.suptitle.html). – michen00 Sep 29 '21 at 20:11
  • 1
    It looks like the "sup" in "suptitle" might stand for super. – Alper Aug 17 '22 at 15:33