How To Annotate The Arrow Position In Matplotlib Using Python

To annotate the arrow position in matplotlib using python we can use the function plt.annotate from matplotlib, more specifically we can use the attribute text to write our text and xytext to declare the position of our text within the plt.anntoate function.

Below, we’ll show you a simple annotation that we did on our arrow.

Annotate The Arrow Position In Matplotlib Using Python
import matplotlib.pyplot as plt

year = [2001, 2002, 2003, 2004, 2005, 2006]
income = [1000, 2000, 13000, 14000, 25000, 26000]

plt.plot(year, income)

plt.annotate(text = "My Money Goal Has Been Reached!",
            xytext = (2002, 21000),
            arrowprops=dict(facecolor='red'),
            xy=(2003,14000))

Line 1: Importing our matplotlib.pyplot package
Line 3-4: Creating our data
Line 6: Plotting our line graph with plt.plot
Line 8-9: using our plt.annotate we declared our text attribute with its position xytext (inside the brackets we need to declare the x and y axis value of our position.
Line 10 – 11: Using arrowprops and xy we create our arrow

As you can see, we can just use the attribute text and xytext within the same function plt.anntoate of our arrow to create our text annotation. By doing so, the arrow will follow our annotated text.