Skip to Content

How to Add Title to a Plot in Matplotlib? (Code Examples with Output)

The matplotlib module in Python comes with a number of useful functions to help us plot data and customize the plots according to our requirements. In this tutorial, we will look at how to set the title of a plot in matplotlib with the help of some examples.

Using the matplotlib.pyplot.title() function

add title to a plot in matplotlib

You can use the title() function available in maplotlib.pyplot to add (or set) title to a plot in matplotlib. The following is the syntax –

import matplotlib.pyplot as plt

# plot your data
plt.scatter(x, y)

# add title to your plot
plt.title("Plot Title")

Let’s now look at some examples of using the above syntax. We’ll plot the US dollar to Indian rupee conversion rates from 2011 to 2020 on a scatter plot and then add a title to it using the method mentioned above.

Example 1 – Add title to a plot

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]

# plot x and y on scatter plot
plt.scatter(x, y)

Output:

the resulting scatter plot without any title

You can see that by default, there is no title set for the plot. Let’s now again plot the same plot but with a title using the matplotlib.pyplot.title() function.

# 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]

# plot x and y on scatter plot
plt.scatter(x, y)
# set plot title
plt.title("USD to INR conversion rate")

Output:

the resulting plot with the given title

The plot now has the title “USD to INR conversion rate”.

Example 2 – Horizontally align the title in matplotlib plot

By default, the title for a plot is “center” aligned. You can also horizontally align the title to “left” and “right” using the loc parameter of the matplotlib.pyplot.title() function.

Let’s plot the same plot as above but with the title aligned to the left.

# plot x and y on scatter plot
plt.scatter(x, y)
# set plot title
plt.title("USD to INR conversion rate", loc="left")

Output:

resulting plot with a left aligned title

Now, let’s align the title to the right.

# plot x and y on scatter plot
plt.scatter(x, y)
# set plot title
plt.title("USD to INR conversion rate", loc="right")

Output:

resulting plot with right aligned title

Example 3 – Modify the font size of the plot title

You can also modify the font and its other properties in the title using the fontdict parameter.

# plot x and y on scatter plot
plt.scatter(x, y)
# set plot title
plt.title("USD to INR conversion rate", fontdict={'fontsize': 20, 'fontweight': 'bold'})

Output:

resulting plot with a formatted title

Here, we customized the fontsize and the fontweight of the title in the plot.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.