15.1 C
London
Friday, July 5, 2024
HomePythonMatplotlib in PythonHow to Add Title to Subplots in Matplotlib (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories