How to set a single, main title above all the subplots with Pyplot?

Jakub M. picture Jakub M. · Aug 15, 2011 · Viewed 231k times · Source

I am using pyplot. I have 4 subplots. How to set a single, main title above all the subplots? title() sets it above the last subplot.

Answer

unutbu picture unutbu · Aug 15, 2011

Use pyplot.suptitle or Figure.suptitle:

import matplotlib.pyplot as plt
import numpy as np

fig=plt.figure()
data=np.arange(900).reshape((30,30))
for i in range(1,5):
    ax=fig.add_subplot(2,2,i)        
    ax.imshow(data)

fig.suptitle('Main title') # or plt.suptitle('Main title')
plt.show()

enter image description here