Matplotlib - Title location


Matplotlib - Title Location

To set a specific location or position for the title of the plot in Matplotlib, you can use loc parameter of the title() function.

To place the title on the left side, use loc='left' as shown in the following.

plt.title('Sample Plot', loc='left')
Matplotlib - Title location = "left"

Other possible values for loc parameter are 'center', and 'right'.

By default the location of the title in Matplotlib is 'center'.

1. Setting Title Location with "left" in Matplotlib

In the following program, we shall place the title at the left side using loc parameter of the title() function.

Python Program

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [20, 30, 55, 70, 60]

# Plot line
plt.plot(x, y, marker='o')

# Set title, at left
plt.title('Sample Plot', loc='left')

# Show the plot
plt.show()

Output

Matplotlib - Title location = "left"

2. Setting Title Location with "center" in Matplotlib

In the following program, we shall place the title at the center using loc parameter of the title() function.

Python Program

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [20, 30, 55, 70, 60]

# Plot line
plt.plot(x, y, marker='o')

# Set title, at center
plt.title('Sample Plot', loc='center')

# Show the plot
plt.show()

Output

Matplotlib - Title location = "center"

3. Setting Title Location with "right" in Matplotlib

In the following program, we shall place the title at the right side using loc parameter of the title() function.

Python Program

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [20, 30, 55, 70, 60]

# Plot line
plt.plot(x, y, marker='o')

# Set title, at right
plt.title('Sample Plot', loc='right')

# Show the plot
plt.show()

Output

Matplotlib - Title location = "right"