Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In matplotlib, what's the difference between title() and suptitle()?

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? Thanks!

like image 962
pyan Avatar asked May 07 '15 18:05

pyan


People also ask

What is Suptitle?

Subtitles are translated captions of audio files, often foreign language films, motion pictures, or television programs. Subtitles transcribe a film's native language to the audience's language.

How do you use Suptitle in Python?

The suptitle() function in pyplot module of the matplotlib library is used to add a title to the figure. Parameters: This function will have following parameters: t : The title text you want to add to your graph. x : The x location of the text in figure coordinates.

Which method is used to add title to the subplots using Matplotlib?

We can also add title to subplots in Matplotlib using title. set_text() method, in similar way to set_title() method.


1 Answers

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

like image 94
xnx Avatar answered Sep 21 '22 20:09

xnx