Home » How to Add Title to Subplots in Matplotlib (With Examples)

How to Add Title to Subplots in Matplotlib (With Examples)

by Tutor Aspire

You can use the following basic syntax to add a title to a subplot in Matplotlib:

ax[0, 1].set_title('Subplot Title')

The following examples shows how to use this syntax in practice.

Example 1: Add Titles to Subplots in Matplotlib

The following code shows how to create a grid of 2×2 subplots and specify the title of each subplot:

import matplotlib.pyplot as plt

#define subplots
fig, ax = plt.subplots(2, 2)

#define subplot titles
ax[0, 1].set_title('First Subplot')
ax[0, 1].set_title('Second Subplot')
ax[1, 0].set_title('Third Subplot')
ax[1, 1].set_title('Fourth Subplot')

Notice that each subplot has a unique title.

Example 2: Add Customized Titles to Subplots in Matplotlib

We can use the following arguments to customize the titles of the subplots:

  • fontsize: The font size of the title
  • loc: The location of the title (“left”, “center”, “right”)
  • x, y: The (x, y) coordinates of the title
  • color: The font color of the title
  • fontweight: The font weight of the title

The following code shows how to use these arguments in practice:

import matplotlib.pyplot as plt

#define subplots
fig, ax = plt.subplots(2, 2)

#define subplot titles
ax[0, 0].set_title('First Subplot', fontsize=18, loc='left')
ax[0, 1].set_title('Second Subplot', x=.75, y=.9)
ax[1, 0].set_title('Third Subplot', color='red')
ax[1, 1].set_title('Fourth Subplot', fontweight='bold')

Using these various arguments, you can customize the subplot titles to look however you’d like.

Additional Resources

The following tutorials explain how to perform other common operations in Matplotlib:

How to Adjust Subplot Size in Matplotlib
How to Adjust Spacing Between Matplotlib Subplots
How to Adjust Title Position in Matplotlib

You may also like