Adding arrows to our plots with the annotate method

The annotate method has a lot of arguments, as seen in the following code:

# Add an arrow
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(facecolor='k'))

The first argument in the preceding code is an empty string that will be displayed. xy tells us where the data coordinates are placed, which is the head. xytext tells us where the text of the tail of the arrow will be placed and the arrowprops argument explains without specifying a color for the arrow—it only uses text that will be displayed. We will start by looking at the arrow itself since we already know how to add text.

As you can see in the following output, we get a nice black arrow. The tip of it is at PI over (2, 1), pointing at the first peak of our sine curve and the tail of it is at (5, 0), where we want our xytext:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset