# How To Annotate Graphs In Matplotlib Using Python (7 Examples)

By inputting annotations onto our graphs we can make our chart easier to interpret and highlight key data. Matplotlib allows you to annotate your graphs in several ways, this includes objects such as text, arrows, lines, and many more.

That is why in this article we will highlight 7 ways that you can annotate your own graphs in matplotlib using python.

## When should we annotate graphs

We should annotate our graphs in every chance we get, to further convey an argument, highlight specific points, or even how to read the graph.

## Annotate using text

Matplotlib offers the ability to place text within a chart. The only condition is it requires the positioning co-ordinate of the x and y-axis to place the text.

### 1. Annotate graph: plt.annotate()

To input text using matplotlib’s “plt.annotate()” we need to declare two things, which is the “xy” coordinates which tells matplotlib where we want to input our text and the “s” attribute.

There is also an added attribute aswell, this is called the “arrowprops” attribute, which basically allows us to input an arrow pointing towards a specific point in our graph.

If arrows and texts are used within the “plt.annotate()” function, you can also use two xy coordinates, one for the arrow and the other for the text. This can be declared via “xy()” and “xytext()” respectively.

These are the following parameters used:

• s  : The text of the annotation
• xy : The point (x,y) to annotate
• xytext : The position (x,y) to place the text at (If None, defaults to xy)
• arrowprops : The properties used to draw an arrow between the positions xy and xytext
``````#input annotation
plt.annotate(
# Label and coordinate
'My Money Goal Has been Reached!', xy=(2003, 14000), xytext=(2002, 20000),
#Arrow Pointer
arrowprops=dict(facecolor='red'))``````

### 2. Annotate graph: ax.text() or plt.text()

Another way to annotate text is to use the function “ax.text()” which is also known as Axes.text(). This function is a much simpler way to input text, as ax.text() only adds the text to the Axes at locations xy in data coordinates. This exact function can also be called using plt.text() aswell.

• x  : The x axis position to place text
• y : The y axis position to place text
• s : String to input text
• fontsize: change the size of the font
• ha: horizontal alignment
• va: vertical alignment
``ax.text(0.5, 0.5, 'Hello World!', size=24, ha='center', va='center')``

### 3. Placing a text box: ax.text()

Similarly, you can also place text boxes with our “ax.text()” function, the text box is created through the use of the “bbox” function. The only difference between our previous step and this current one is we require to create a new variable called “textbox” which helps create our text box.

Below is the exact function to use for the current text box used.

``````# Create our text box
textbox = dict(boxstyle='round', facecolor='wheat', alpha=0.5)

# place a text box in upper left in axes coords input our text box
ax.text(0.05, 0.95, 'This is a histogram', transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=textbox)``````

## Annotate using shapes

Another way to annotate your graph is to input shapes, this can include things such as a vertical line, a rectangle and many more objects which highlight a spot within a specific graph. Combining shapes with text annotations will definitely create a better solution to annotating graphs.

### 4. Annotate using a vertical line: plt.axvline()

Matplotlib allows the ability to input a vertical line to highlight a specific spot of a graph, by doing so we can identify key statistics such as in our current graph where I used a line annotation and also a text annotation to visualize the significance of the 2008 Great financial crash.

To create a vertical line annotation we can use the function plt.axvline() this basically creates a vertical line from a specified x-axis spot.

• x  : The x axis position to place vertical line
• color : Color of the line graph
• linestyle : Line graph style
``plt.axvline(2007, color='r', linestyle='dashed')``

### 5. Annotate using a horizontal line: plt.axhline()

Similarly, you can also annotate using a horizontal line, this can simply be created using the function plt.axhline(). The only difference in the parameters between this function and the previous plt.axvline() is the requirement of the y axis position.

• y  : The y axis position to place vertical line
• color : Color of the line graph
• linestyle : Line graph style
``plt.axhline(60000, color='r', linestyle='dashed')``

### 6. Highlight using a rectangle: ax.add_patch(Rectangle())

To input a rectangle in matplotlib we need to import our rectangle package, this can be declared using the function “from matplotlib.patches import Rectangle“. Afterwards, we can now use our function “ax.add_patch(Rectangle())”.

These are the following parameters used in the function

• xy  : The xy axis starting point for the lower left side of the rectangle
• width: the width span of the rectangle
• height: the height span of the rectangle
• color: the color of the rectangle
``````ax.add_patch(Rectangle((2005, 55000), 6, 5000, color="red"))
``````

### 7. Highlight using a circle: patches.Circle()

We can also create a ‘looking glass’ effect using matplotlib. To do this we need to import the package “matplotlib.patches as patches” this library stores our circle function (patches.Circle()).

These are the following parameters of our function

• xy  : The xy axis of the center of the circle
• radius: the size of the circle
• fc: the color of the rectangle
• alpha: transparency of the circle