Matplotlib - Arrow Demo



An "arrow demo" in general is a graphical representation where arrows are used to convey information, point up a direction, or highlight specific points. Arrows are commonly used to indicate vector quantities, movement, or relationships between elements.

For instance, if you are analyzing sales data over several months, you can visually highlight a significant increase in sales during a specific month as shown below −

Arrow Demo

Arrow Demo in Matplotlib

We can create an arrow demo in Matplotlib using the arrow() function. This function allows you to create arrows on a plot with specified starting and ending points, arrowhead style, color, and linestyle.

The arrow() Function

The arrow() function in Matplotlib takes parameters for the starting point (x, y), the arrow direction (dx, dy), and optional properties such as color and style.

Following is the syntax of the arrow() function in Matplotlib −

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

Where,

  • x, y is the starting point of the arrow.
  • dx, dy is the change in x and y that defines the arrow direction.
  • **kwargs is the additional keyword arguments for customization (e.g., color, linestyle, arrowhead style).

Arrow Demo with Custom Color

We can create an arrow demo with custom color, graphically displaying arrows in a plot, allowing you to choose specific colors to fill the arrowhead.

Example

In the following example, we are creating a arrow starting from the point (0, 0) and pointing in the direction of (1, 1). We then set the arrow to have a red fill color (fc) while keeping a black edge color (ec)−

import matplotlib.pyplot as plt
# Starting point
x = 0
y = 0

# Arrow direction
dx = 1
dy = 1
# Plotting an arrow with a custom color
plt.arrow(x, y, dx, dy, head_width=0.05, head_length=0.1, fc='red', ec='black')
plt.title('Arrow Demo with Custom Color')
plt.show()

Output

After executing the above code, we get the following output −

Arrow Demo with Custom Color

Double-Headed Arrow Demo

A double-headed arrow demo is like drawing a line on a graph, but instead of just one arrowhead at the end, you have arrowheads at both ends. This is useful when you want to show a two-way connection or movement in a plot, making it clear that things go in both directions.

Example

In here, we are creating a double-headed arrow by plotting two arrows: one from (0, 0) to (1, 1) and another in the opposite direction. We are filling the arrows with same fill color (fc) and edge color (ec) −

import matplotlib.pyplot as plt
# Starting point
x = 0
y = 0

# Arrow direction
dx = 1
dy = 1

# Plotting a double-headed arrow
plt.arrow(x, y, dx, dy, head_width=0.05, head_length=0.1, fc='green', ec='black')
plt.arrow(x+dx, y+dy, -dx, -dy, head_width=0.05, head_length=0.1, fc='green', ec='black')
plt.title('Double-Headed Arrow Demo')
plt.show()

Output

Following is the output of the above code −

Double-Headed Arrow Demo

Arrow Demo with Dashed Line

Arrow demo with dashed line refers to outlining the arrow with a dashed line instead of a continuous line. We can draw a dashed line in matplotlib by passing the linestyle argument and setting it to 'dashed' when drawing the arrow.

Example

Now, we are customizing the linestyle of the arrow as a dashed line, having orange fill color and blue edge color −

import matplotlib.pyplot as plt
# Starting point
x = 0
y = 0

# Arrow direction
dx = 1
dy = 1

# Plotting an arrow with a dashed line
plt.arrow(x, y, dx, dy, head_width=0.05, head_length=0.1, fc='orange', ec='blue', linestyle='dashed')
plt.title('Arrow Demo with Dashed Line')
plt.show()

Output

Output of the above code is as follows −

Arrow Demo with Dashed Line

Arrow Demo with Custom Arrowhead Shape

While creating an arrow in a plot, we can also customize the shape of the arrowheads. Instead of the standard full arrowhead, you can choose a unique shape, like a right-pointing triangle or a left-pointing triangle, positioned at the right-half or the left-half respectively at the end of the arrow line.

Example

In the example below, we are customizing the arrowhead shape to be a right-pointing triangle i.e. the arrowhead is positioned at the right end of the arrow line −

import matplotlib.pyplot as plt
# Starting point
x = 0
y = 0

# Arrow direction
dx = 1
dy = 1

# Plotting an arrow with a custom arrowhead shape
plt.arrow(x, y, dx, dy, head_width=0.05, head_length=0.1, fc='purple', ec='green', shape='right')
plt.title('Arrow Demo with Custom Arrowhead Shape')
plt.show()

Output

The output obtained is as shown below −

Custom Arrowhead Shape
Advertisements