Customizing the appearance of the annotations

There are many different options available to annotate a particular region.

  1. We can also pass arrow-style attributes. So, if we insert head_length=2 and head_width=2, we get the following output:
# 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,head_length=2,head_width=2'))

The preceding code gives us the following output:

So far, we have been looking at very straight arrows—with annotate, we have the ability to actually curve these arrows along arcs. This is very useful as it helps to guide the viewer's eye, not just to the tips where we have the text and the head, but along the plot as well.

  1. Hence, often, if we want to annotate things that also show flow or curve, we could curve the arrow. We will insert 0.5, which gives a curve at an angle of 0.5 radians, as shown here:
# Customize the arrow (connectionstyle w/ angle3, arc3)
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',
connectionstyle='arc3,rad=0.5'))

The output can be shown as follows:

When we insert the angle3 option, it takes two keyword arguments: angleA and angleB.

As can be seen in the following figure, these angles do not come in as separate keys; they come after commas in the string.
  1. These angles, unlike for arc3, are in degrees, so they describe the angle at the tip and the angle at the text:
# Customize the arrow (connectionstyle w/ angle3, arc3)
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',
connectionstyle='angle3,angleA=-30,angleB=30'))

Hence here, we can see the twist in the arrow:

We often find that parts of the plot are covered up by the arrow annotation. If we want to show the curve of some other part of the data, we can use angle3 or arc3 as a quick and easy way of doing that.

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

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