Categories

# Add Title To Matplotlib Plot Using Python

In this tutorial, we will learn how to add title to a Matplotlib plot using Python. The Matplotlib library gives us a special function that we can use to achieve this. We are going to learn more about this function and how we can use it. But before that, let us first understand what a title is? Why do we need it?

## What Is A Title?

A title is a short heading describing the gist of a content. But in our case, we can simply say that a title in Matplotlib plot will give us a hint of what the plot is all about.

## How To Add Title To Matplotlib Plot?

So now that we understand what a title is, let us see how we can add it to our Matplotlib plot.

But before we begin, let us first take a look at our last tutorial. In that tutorial, we had just learned how to add labels to the axes of the plot. The output plot of that tutorial looked like this:

Although the above plot looks complete, it is still missing one important element. We are still not able to make out what this plot is all about!

This is because the plot is not having a title. If there was a title in that plot, it would have told us what it is all about.

So now that we know the missing element, let us learn how to add it.

But first, let us get the original code from our previous article that gave us the above plot. It looks like this:

``````import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x])
plt.plot(x, [xi*2 for xi in x])
plt.plot(x, [xi*3 for xi in x])

plt.grid()
plt.axis([0, 20, 0 , 40])
plt.xlabel('This is the X axis label')
plt.ylabel('This is the Y axis label')
plt.show()``````

So, with the above code, it is now time for us to learn about a new function of Matplotlib. This is the function that will help us add title to our plot. This function is part of the sub module pyplot of Matplotlib.

### Introducing the Matplotlib title function

The function is simply called title(). The signature of this function looks like this:

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

From the above function signature, we can see that it can accept a few arguments.

The first argument label will accept a string of text. So we can see that it is text that would get displayed as our title.

The second argument will accept the font information. So we can actually chose our font type here. We can also choose its size, style etc. Take a look at its documentation again to know more.

Next, the third parameter loc will help us in positioning our title. It can be set to one of the following positions: center, left or right. It’s default value is center.

Finally, we will take a look at it’s last parameter pad. It is used to add padding to the title text. So this padding is applied from the top of the axes.

With this much of information in our hand, we can now add our own title to the above plot. Since this plot is a dummy plot, let us use the title “Dummy Plot” for it. So with this, our code will now look like this:

``````import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x])
plt.plot(x, [xi*2 for xi in x])
plt.plot(x, [xi*3 for xi in x])

plt.grid()
plt.axis([0, 20, 0 , 40])
plt.xlabel('This is the X axis label')
plt.ylabel('This is the Y axis label')
plt.title('Dummy Plot')
plt.show()``````

From the above code, you can see that we have passed only one argument. The title of the plot with text ‘Dummy Plot’. So we have left all other parameter values to remain at its default values. With this, our new Matplotlib output plot looks like this:

This concludes our current tutorial. If you still have any more doubts on this, do let us know in the comment below. We will be happy to help.

Until next time, ciao! 🙂 ## By Amar Nath

You can buy me a cup of coffee at: