Draw an arrow using matplotlib in Python

In this article, we are going to learn how to draw an arrow inside the plot using matplotlib in Python. An arrow is drawn in the plot to show the specific part of the graph or plot, however, to annotate the plot or graph we add the textboxes inside the plot.

Arrow is drawn inside the plot using the inbuilt function pyplot.annotate() and if we want to draw the only arrow as a plot then we have to work with the pylab module. Let’s understand with some example:-

  • Plotting of the only arrow inside the plot
import pylab as p

#p.arrow( x, y, dx, dy, **kwargs )
p.arrow( 0.5, 0.8, 0.0, -0.2, fc="k", ec="k",head_width=0.05, head_length=0.1 )


The output plot will looks like the figure:

arrow inside the plot matplotlib in Python

In the above example, we first imported the pylab module and then we use the function pylab.arrow() and we passed the required arguments provided in the code in the form of a comment.

The first argument is x-coordinate, the second argument is y-coordinate of the arrow and the third and fourth arguments are the distance from the x-coordinate(x+dx) and distance from the y-coordinate(y+dy), and then we provided the fc=face color and ec=edge color and then we provided the head_width and head_length. Then we use the pylab.show() function to show the drawn arrow.

  • Plotting of arrow inside the plot with figure
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-3.9, 5, 1019)
Y = .24 * (X + 3.8) * (X + 0.9) * (X - 1.8)
plt.annotate('Brackmard minimum',
ha = 'center', va = 'bottom',
xytext = (-1.8, 3.5),xy = (0.69, - 1.7),arrowprops = {'facecolor' : 'black'})
plt.plot(X, Y)

Below is the given output figure:

arrow inside the plot with figure using matplotlib in Python


In this example, we first imported the required module such as matplotlib and numpy and then we created a numpy linspace from -3.9 to 5 and stored in X and then we established the relation between X and Y.

Also, read:

Then we use the plt.annotate() function to annotate(write some text inside the plot) the plot and provided the x-coordinate and y-coordinate of the arrow, and then we provided the arrow properties like face color and then we plotted the graph using plt.plot() and plt.show().

Leave a Reply

Your email address will not be published. Required fields are marked *