Adding some text to the arrows

We will add some text under the arrow, as shown in the following points:

  1. Change the empty string to a string that says 1st peak:
# Add text
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("1st peak", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(facecolor='k'))

We will thus get some text that begins at (5, 0), and the tip of our arrow pointing at PI over (2, 1):

  1. This text takes all of the usual keyword arguments that the text method itself takes, so we will include color= red, weight =bold, style=italic, and all of those will be passed on, as follows:
# Customize text
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), color='red', weight='bold', style='italic', arrowprops=dict(facecolor='k'))

Here, we get a nice, bold, italic text, as follows:

  1. The arrow itself is customized with arrowprops=dict, so, for example, when we pass shrink=0.5, we cut off the bottom half of the arrow, as shown here:
# Customize the arrow (width, headwidth, headlength, shrink)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(facecolor='k', headwidth=50, headlength=40, shrink=0.5))

We get the following output:

  1. For a wide arrow with a width of ten, insert width=10, and we get the following output:
# Customize the arrow (width, headwidth, headlength, shrink)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(facecolor='k', width=10, shrink=0.5))

The output can be seen as follows:

  1. For changing the width of the head argument, include headwidth=50—there, you can see that the head of the arrow gets very big:
# Customize the arrow (width, headwidth, headlength, shrink)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(facecolor='k', headwidth=50, shrink=0.5))

We get the following output:

  1. The length of the head can also be changed, so the head itself is manipulated with these keyword arguments, as shown here:
# Customize the arrow (width, headwidth, headlength, shrink)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(facecolor='k', headwidth=50, headlength=40, shrink=0.5))

We get the following output:

  1. There is also an ability to pass different kinds of arrow styles. By default, the arrow style key is not passed in arrowprops=dict—it will use arrowstyle='simple', as shown in the following code:
# Customize the arrow (arrowstyles with attributes)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(arrowstyle='simple'))

We get the following output:

  1. We can also have a fancy arrow that gives a wedge, as shown here:
# Customize the arrow (arrowstyles with attributes)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(arrowstyle='fancy'))

We get the following output:

  1. Series punctuation can be included to make simpler line-type arrows, as shown here:
# Customize the arrow (arrowstyles with attributes)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(arrowstyle='->'))

We get the following output:

  1. Also, flat edges can be added in order to show distance, as shown here:
# Customize the arrow (arrowstyles with attributes)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(arrowstyle='|-|'))

The output can be seen as follows:

  1. We can also include boxy edges, as shown in the following code:
# Customize the arrow (arrowstyles with attributes)
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.annotate("look!", xy=(np.pi/2, 1), xytext=(5,0), arrowprops=dict(arrowstyle=']-['))

We get the following output:

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

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