Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to Adjust Title Position in Matplotlib?

How to Adjust Title Position in Matplotlib?

In this article, you learn how to modify the Title position in matplotlib in Python.

Method 1: Using matplotlib.pyplot.title() function

The title() method in matplotlib module is used to specify title of the visualization depicted and displays the title using various attributes.

Syntax:

matplotlib.pyplot.title(label, fontdict=None, loc=’center’, pad=None, **kwargs)

Example 1:

In this example, we will look at how to give a title, Matplotlib provides a function title() which is used to give a title for the plots.

Python3




#import matplotlib
import matplotlib.pyplot as plt
  
# Points to mark
plt.plot([1, 2, 3, 4, 5], [1, 4, 6, 14, 25])
  
# X label
plt.xlabel('X-axis')
  
# Y label
plt.ylabel('Y-axis')
  
# Title
plt.title('Title')


Output:

By default, TitleTitle is placed in the center; it is pretty simple to change them. 

Example 2:

In this example, we have placedTitleTitle to the right of the plot using matplotlib.pyplot.title() function by initializing the argument as right. 

Python3




#import matplotlib
import matplotlib.pyplot as plt
  
# Points to mark
plt.plot([1, 2, 3, 4, 5], [1, 4, 6, 14, 25])
  
# X label
plt.xlabel('X-axis')
  
# Y label
plt.ylabel('Y-axis')
  
# Title
plt.title('Title', loc='right')


Output:

right

Example 3:

In this example, we have placedTitleTitle to the left of the plot using matplotlib.pyplot.title() function by initializing the argument as left. 

Python3




#import matplotlib
import matplotlib.pyplot as plt
  
# Points to mark
plt.plot([1, 2, 3, 4, 5], [1, 4, 6, 14, 25])
  
# X label
plt.xlabel('X-axis')
  
# Y label
plt.ylabel('Y-axis')
  
# Title
plt.title('Title', loc='left')


Output:

Method 2: Changing the location of title with x and y coordinates

In this method, we will place title inside the plot. Instead of giving the location in the “loc” parameter, we will give the exact location where it should be placed by using X and Y coordinates. 

Syntax:

 matplotlib.pyplot.title('Title', x=value, y=value)

Example:

In this example, we will be assigning the value of the x and y at the position where the title is to be placed in the python programming language.

Python3




#import matplotlib
import matplotlib.pyplot as plt
  
# Points to mark
plt.plot([1, 2, 3, 4, 5], [1, 4, 6, 14, 25])
  
# X label
plt.xlabel('X-axis')
  
# Y label
plt.ylabel('Y-axis')
  
# Title
plt.title('Title', x=0.4, y=0.8)


Output:

Method 3: Changing the location of title with pad parameter

In this method, we will be using the pad argument of the title() function to change the title location in the given plot in the python programming language.

Syntax:

matplotlib.pyplot.title('Title', pad=value)

Example:

In this example, We will elevateTitleTitle by using the “pad” parameter. The offset of the Title from the top of the axes, in points. The default value is none.

Python3




#import matplotlib
import matplotlib.pyplot as plt
  
# Points to mark
plt.plot([1, 2, 3, 4, 5], [1, 4, 6, 14, 25])
  
# X label
plt.xlabel('X-axis')
  
# Y label
plt.ylabel('Y-axis')
  
# Title with pad
plt.title('Title', pad=50)


Output:

RELATED ARTICLES

Most Popular

Recent Comments