Center a title above subplots in Matplotlib

Python

Matplotlib

Figure

Create centered title

Luc B.

Python

Matplotlib

When creating a figure with multiple plots, users often want one title centered over all plots instead of individual titles.

Code Example

Use the plt.suptitle() function to create a centered figure title.

import matplotlib.pyplot as plt

# Create a figure with multiple plots
plt.subplot(121)
plt.text(0.4, 0.4, "L", fontsize=50)
plt.subplot(122)
plt.text(0.4, 0.4, "R", fontsize=50)

# Create a centered title here
plt.suptitle("Centered Title", fontsize=20)

plt.show()

png

More Examples

Object Oriented Interface

The same functionality is easily accomplished with Matplotlib's object oriented interface.

fig = plt.figure()

# Create a figure with multiple plots
l = fig.add_subplot(121)
l.text(0.4, 0.4, "L", fontsize=50)
r = fig.add_subplot(122)
r.text(0.4, 0.4, "R", fontsize=50)

# Create a centered title here
fig.suptitle("Centered Title", fontsize=20)

plt.show()

png