Categories

## Matplotlib Annotate Text In A Plot Using Python

In this article, we will learn how to annotate text in Matplotlib plot using Python. But if we only add annotation text to a plot, it will not look that great. Because we want that annotation to point to a particular location in the plot. Right? So we will also be looking at how we can add arrows to go with these annotations. By using arrows, we will be able to point it to a location on the plot as well.

Now does that sound good for you? Great! So then let u begin!

First thing first. Let us learn what an annotation is. Alright? Because knowing what an annotation does to a plot will help us know how to use it. Right? So let us start fro there.

## What Is Annotation?

Annotation is a piece of text that we can add to a plot to add give some explanation. So that is all there is to it.

But then you may ask why we want to text annotate a Matplotlib? After all, we already have a way to add text inside a plot in Matplotlib. Right? We have already seen how we can do that in our earlier article. So then why do we need annotation as well?

Well, there is a simple reason for that. So you see, using text( ) function provided by Matplotlib is fine for simple texts. But for larger texts, we want to go for annotations because we can use arrows here! So that way we can use larger texts but still point this text a point on the plot using arrows!

So that is the real benefit of using annotated text in Matplotlib plots rather than normal text( ) function. I hope this is clear to you by now!

So with that, let us see how we can text annotate a point in the Matplotlib plot.

## How To Use Matplotlib To Annotate Text In A Plot

So how do we add annotation to a Matplotlib plot? Well using the annotate( ) function from the pyplot module, of course!

So the pyplot module provides us with an annotate( ) function as well! So using this, we can add annotations to our plot. But how does the signature of this function look like? Let us take a look at it:

``annotate(text, xy, xytext, arrowprops)``

So this is the basic signature of the annotate( ) function. But this signature is not all of it. It is missing few more arguments. But they are all optional. So we have skipped what is not required.

So what do these arguments mean? To understand, let us take a look at them one by one:

``````text - This is the annotation text
xy - This is the x & y co-ordinates of the point on our plot to which we will be adding the annotation
xytext - This is the x & y co-ordinates where we want our annotated text to appear
arrowprops - This will define the properties of our arrow``````

I know this is all looking overwhelming. But it will become clear to you once we looked at an example code using that. Alright? So let us look at that right now!

## Matplotlib Annotate Text Example

So here is our example code:

``````import matplotlib.pyplot as plt
y = [8, 10, 11, 12, 10, 9, 10, 8, 7, 11, 10, 9]
plt.plot(y)
plt.annotate('this is the point I want to point!',
xy=(4, 10), xytext=(5, 10.75), arrowprops=dict(facecolor='red'))
plt.show()``````

As you can see, we are using a simple data set y with 12 values in it. We are then calling the normal plot( ) function to plot this data. But the real meat is in our 4th line where we are using our annotate( ) function.

So as you can see, we are passing a number of parameters here. The first is the text parameter which simply reads “this is the point I want to point!”.

The next is the xy parameter which is the point we want to annotate. This is followed by the xytext parameter which allows us to set the location where we wan our text to appear.

And finally we have the arrowprops parameter which allows us to set the arrow properties.

So using this code we get the following plot:

So there you have it. This is how you can use Python Matplotlib library to create a plot with annotated text. I hope it was easy enough for you. But if you have any questions, do let me know in the comments below and I will be happy to help.

So with that, I will end this tutorial now. Hope you have a nice day! ðŸ™‚