Matplotlib Arrow() Function With Examples

Hello programmers, in today’s article, we will discuss Matplotlib Arrow() in Python. Matplotlib is a powerful plotting library used for working with Python and NumPy. And for making statistical interference, it is necessary to visualize data, and Matplotlib is very useful. It provides a MATLAB-like interface only difference is that it uses Python and is open source.

Syntax of Matplotlib Arrow() in python:

matplotlib.pyplot.arrow(x, y, dx, dy, **kwargs) 

Parameters:

  • x and y are the coordinates of the arrow base.
  • dx and dy are the length of the arrow along the x and y-direction, respectively.
  • **kwargs are optional arguments that help control the arrow’s construction and properties, like adding color to the arrow, changing the width of the arrow, etc.
  • Constructor Arguments for **kwargs: width: float (default: 0.001) – width of full arrow tail
  • length_includes_head: bool (default: False) – True if head is to be counted in calculating the length.
  • head_width: float or None (default: 3*width) – Total width of the full arrow head
  • head_length: float or None (default: 1.5 * head_width) – Length of arrow head
  • shape: [‘full’, ‘left’, ‘right’] (default: ‘full’) – Draw the left-half, right-half, or full arrow
  • overhang: float (default: 0) – Fraction that the arrow is swept back (0 overhang means triangular shape). Can be negative or greater than one.
  • head_starts_at_zero: bool (default: False) – If True, the head starts being drawn at coordinate 0 instead of ending at coordinate 0.

Return of Matplotlib Arrow() function:

Returns an Arrow depending on our desired parameters that we provide as inputs.

Plot Arrow using Matplotlib Arrow() in Python

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#add arrow to plot
plt.arrow(x=4, y=18, dx=2, dy=5, width=.06) 
  
#display plot 
plt.show()

Output:

Explanation:

In the above example, an arrow is created where x and y parameters are set to 4 and 8. The length of the arrow to be created is passed by setting dx and dy to 2 and 5, respectively. And the width is passed as **kwargs constructor argument, which controls the arrow’s width to be created.

Note: dx is set to 0 to create a vertical arrow and dy to 0 to create a horizontal arrow.

Arrow styling using Matplotlib Arrow()

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#add arrow to plot
plt.arrow(x=4, y=18, dx=0, dy=5, width=.08, facecolor='red') 
  
#display plot 
plt.show()

Output:

Arrow styling using Matplotlib Arrow()

Explanation:

There are various ways of styling arrows. These different styling properties are applied by passing it as **kwargs argument. In the above example, the face color of the arrow being created is set to red. And, here we get a vertical arrow as we have set the value of dx to 0.

Adding Annotations to Arrows

import matplotlib.pyplot as plt

#define two arrays for plotting
A = [3, 5, 5, 6, 7, 8]
B = [12, 14, 17, 20, 22, 27]

#add arrow to plot
plt.arrow(x=4, y=18, dx=0, dy=5, width=.08) 

#add annotation
plt.annotate('General direction', xy = (3.3, 17))
  
#display plot
plt.show() 

Output:

Adding Annotations to Arrows

Explanation:

Annotation, in the simplest form, means adding a text at a point (x, y). To annotate an arrow means adding text around the arrow at a specific x and y coordinate value.

Syntax of Annotate function:

matplotlib.pyplot.annotate(text, xy,*args,**kwargs) 

Where text to be added x and y are the point to annotate and, *args and **kwargs are optional parameters that control annotation properties. In the above example, ‘General direction’ text is added at x = 3.3 and y = 17.

Conclusion

This article provides an obvious understanding of Matplotlib arrow() in python. All are included in this article, from plotting an arrow to styling it and adding annotations. Implementation of arrows in the python program along with examples is also well defined here.

However, if you have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments