In this tutorial, we will look at how to add a different title to each subplot in matplotlib with the help of some examples.

When you’re working with a single plot, you can use the matplot.pyplot
object’s title()
method to set its title. To set the titles for subplots, however, you have to use the respective subplot’s axes object’s set_title()
method. Pass the title as an argument.
The following is the syntax –
# create a plot with two subplots fig, (ax1, ax2) = plt.subplots(1, 2) # add title to subplot1 ax1.set_title("Title of subplot1") # add title to subplot2 ax2.set_title("Title of subplot2")
Let’s now look at some examples of using the above syntax. First, we will a plot with two subplots without any titles.
import matplotlib.pyplot as plt # x values - years x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020] # y values - 1 USD in INR y = [46.67, 53.44, 56.57, 62.33, 62.97, 66.46, 67.79, 70.09, 70.39, 76.38] # create subplots fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # plot the first subplot ax1.scatter(x, y) # add axis labels ax1.set_xlabel('Year') ax1.set_ylabel('1USD in INR') # plot the second subplot ax2.scatter(x, y) # add axis labels ax2.set_xlabel('Year') ax2.set_ylabel('1USD in INR')
Output:

Example 1 – Add a different title to each subplot
Here, we plotted the same scatter plot in both subplots but that is not important. Let’s see how we can separately set the title for each subplot.
# create subplots fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # plot the first subplot ax1.scatter(x, y) # add axis labels ax1.set_xlabel('Year') ax1.set_ylabel('1USD in INR') # add title to subplot ax1.set_title('USD vs INR YoY') # plot the second subplot ax2.scatter(x, y) # add axis labels ax2.set_xlabel('Year') ax2.set_ylabel('1USD in INR') # add title to subplot ax2.set_title('USD vs INR in 2010s')
Output:

Each subplot now has its own title.
Example 2 – Add a title to the entire figure
In case you want to set the title of the entire figure, you can use the fig
object’s suptitle()
method. Let’s add an overall title to the above grid of subplots.
# create subplots fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # plot the first subplot ax1.scatter(x, y) # add axis labels ax1.set_xlabel('Year') ax1.set_ylabel('1USD in INR') # add title to subplot ax1.set_title('USD vs INR YoY') # plot the second subplot ax2.scatter(x, y) # add axis labels ax2.set_xlabel('Year') ax2.set_ylabel('1USD in INR') # add title to subplot ax2.set_title('USD vs INR in 2010s') # add figure title fig.suptitle("Foregin currency performance against USD", fontsize=18)
Output:

We added a title to the entire figure. Note that we additionally passed the font size we want the title to have with the fontsize
parameter.
You might also be interested in –
- Matplolib – Hide Axis in a Plot (Code with Examples)
- Matplotlib – Change the Number of Ticks in a Plot
- Remove Tick Labels from a Plot in Matplotlib
- Set Axis Range (axis limits) in Matplotlib Plots
- Reverse Axes of a Plot in Matplotlib
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.