Open In App

Matplotlib.pyplot.title() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

The title() method in the Matplotlib module is used to specify the title of the visualization depicted and display the title using various attributes. In this article, we will learn about Matplotlib’s title() function with the help of examples.

Matplotlib.pyplot.title() Syntax in Python

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

  • label(str): This argument refers to the actual title text string of the visualization depicted.
  • fontdict(dict) : This argument controls the appearance of the text such as text size, text alignment etc. using a dictionary. Below is the default fontdict:
    • fontdict = {‘fontsize’: rcParams[‘axes.titlesize’],
    • ‘fontweight’ : rcParams[‘axes.titleweight’],
    • verticalalignment’: ‘baseline’,
    • ‘horizontalalignment’: loc}
  • loc(str): This argument refers to the location of the title, takes string values like 'center', 'left' and 'right'.
  • pad(float): This argument refers to the offset of the title from the top of the axes, in points. Its default values in None.
  • **kwargs: This argument refers to the use of other keyword arguments as text properties such as color, fonstyle, linespacing, backgroundcolor, rotation etc.

Return Type: The title() method returns a string that represents the title text itself.

Python Matplotlib.pyplot.title() Function Examples

Below are some examples by which we can understand about Matplotlib title() function in Python:

Generating and Displaying Title of a Simple Linear Graph Using Matplotlib

In this example, using matplotlib.pyplot, a linear graph is depicted with x and y coordinates, and its title “Linear graph” is displayed using matplotlib.pyplot.title(). Assignment of the label argument is the minimum requirement to display the title of a visualization.

Python3




# importing module
import matplotlib.pyplot as plt
 
# assigning x and y coordinates
y = [0, 1, 2, 3, 4, 5]
x = [0, 5, 10, 15, 20, 25]
 
# depicting the visualization
plt.plot(x, y, color='green')
plt.xlabel('x')
plt.ylabel('y')
 
# displaying the title
plt.title("Linear graph")
 
plt.show()


Output:

Visualizing the ReLU Function Using Matplotlib.title() Function

In this example, using matplotlib.pyplot, a ReLU function graph is depicted based on given x-coordinates, and its title “ReLU function graph” is displayed with specified styling using matplotlib.pyplot.title().

Python3




# importing module
import matplotlib.pyplot as plt
 
# assigning x and y coordinates
x = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
y = []
 
for i in range(len(x)):
    y.append(max(0, x[i]))
 
# depicting the visualization
plt.plot(x, y, color='green')
plt.xlabel('x')
plt.ylabel('y')
 
# displaying the title
plt.title(label="ReLU function graph",
          fontsize=40,
          color="green")


Output:

Displaying a Bar Graph Title Using Matplotlib.title() Function

In this example, we are using matplotlib.pyplot to depict a bar graph and display its title using matplotlib.pyplot.title(). Here, the fontweight key of the fontdict argument and pad argument is used in the title() method along with the label parameter.

Python3




# importing modules
import matplotlib.pyplot as plt
import numpy as np
 
# assigning x and y coordinates
language = ['C', 'C++', 'Java', 'Python']
users = [80, 60, 130, 150]
 
# depicting the visualization
index = np.arange(len(language))
plt.bar(index, users, color='green')
plt.xlabel('Users')
plt.ylabel('Language')
plt.xticks(index, language)
 
# displaying the title
plt.title(label='Number of Users of a particular Language',
          fontweight=10,
          pad='2.0')


Output:

Displaying a Pie Chart Title Using Matplotlib.title() Function

In this example, we are using matplotlib.pyplot to depict a pie chart and display its title using matplotlib.pyplot.title(). Data visualization of the pie chart, label, fontweight keyword from fontdict and fontstyle(**kwargs) argument(takes string values such as 'italic', 'bold' and 'oblique') is used in the title() method to display the title of the pie chart.

Python3




# importing modules
from matplotlib import pyplot as plt
 
# assigning x and y coordinates
foodPreference = ['Vegetarian', 'Non Vegetarian',
                  'Vegan', 'Eggitarian']
 
consumers = [30, 100, 10, 60]
 
# depicting the visualization
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('equal')
ax.pie(consumers, labels=foodPreference,
       autopct='%1.2f%%')
 
# displaying the title
plt.title(label="Society Food Preference",
          loc="left",
          fontstyle='italic')


Output:

Visualizing a Sinusoidal Signal Over Time Using Matplotlib.title()

In this example, we are using matplotlib.pyplot to visualize a signal in a graph and display its title using matplotlib.pyplot.title(). Here, the label argument is assigned to 'signal' , loc argument is assigned to 'right' and the rotation argument (**kwargs) which takes angle value in degree is assigned to 45 degrees.

Python3




# importing modules
from matplotlib import pyplot
import numpy
 
signalTime = numpy.arange(0, 100, 0.5)
 
# getting the amplitude of the signal
signalAmplitude = numpy.sin(signalTime)
 
# depicting the visualization
pyplot.plot(signalTime, signalAmplitude, color='green')
 
pyplot.xlabel('Time')
pyplot.ylabel('Amplitude')
 
# displaying the title
pyplot.title("Signal",
             loc='right',
             rotation=45)


Output:

Displaying an Image Title Using Matplotlib title() Function

In this example, we are matplotlib.pyplot to show an image and display its title using matplotlib.pyplot.title(). The title of an image is displayed using the title() method having arguments label as "Geeks 4 Geeks", fontsize key from fontdict as '20', backgroundcolor and color are extra parameters having string values 'green' and 'white' respectively.

Python3




# importing modules
from PIL import ImageTk, Image
from matplotlib import pyplot as plt
 
# depicting the visualization
testImage = Image.open('g4g.png')
 
# displaying the title
plt.title("Geeks 4 Geeks",
          fontsize='20',
          backgroundcolor='green',
          color='white')
plt.imshow(testImage)


Output:



Last Updated : 24 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads