”;
What are Arrows in Matplotlib?
In Matplotlib library Arrows refer to the graphical elements used to indicate direction, connection between points and to highlight specific features within plots. Arrows can be added to plots using the plt.arrow() function or incorporated within annotations by using the plt.annotate() function.
Arrows in Matplotlib library are versatile elements used to visually depict directionality, connections or highlights within the plots for aiding in better communication of information in visualizations. We can adjust parameters like coordinates, lengths, colors and styles to suit the specific visualization needs.
The plt.arrow() function in matplotlib library creates arrows between two points on a plot.
Syntax
The following is the syntax and parameters of the plt.arrow() function.
plt.arrow(x, y, dx, dy, kwargs)
Where,
-
x, y − These are the starting point coordinates of the arrow.
-
dx, dy − These are lengths of the arrow in the x and y directions.
-
kwargs − We can add additional keyword arguments to customize arrow properties such as color, width, style etc.
Adding arrows to the line plot
In this example we are plotting a plot by creating the arrows between the given two points on the plot by using the plt.arrow() function. To this function we passed the x, y, dx and dy points as input parameters for creating arrows in those mentioned points. In addition we passed the parameters such as length, width and color of the arrow.
Example
import matplotlib.pyplot as plt # Creating a line plot plt.plot([0, 1], [0, 1]) # Adding an arrow plt.arrow(0.2, 0.2, 0.4, 0.4, head_width=0.05, head_length=0.1, fc=''red'', ec=''blue'') plt.xlabel(''X-axis'') plt.ylabel(''Y-axis'') plt.title(''Arrow created by using plt.arrow() function'') # show grid of the plot plt.grid(True) plt.show()
Output
Adding arrow separately
Here this is another example of creating the arrows. As we know arrows can also be integrated into annotations using plt.annotate() by specifying arrowprops to define arrow styles. In this example ”Arrow Annotation” is connected to point (0.5, 0.5) with an arrow style specified by arrowprops.
Example
import matplotlib.pyplot as plt # Creating a plot plt.plot([0, 1], [0, 1]) # Adding an arrow with annotation plt.annotate(''Arrow Annotation'', xy=(0.5, 0.5), xytext=(0.2, 0.2), arrowprops=dict(facecolor=''yellow'',ec = ''red'', arrowstyle=''->'')) plt.xlabel(''X-axis'') plt.ylabel(''Y-axis'') plt.title(''Arrow Annotation Example'') # Displaying the grid plt.grid(True) plt.show()
Output
Plotting distance arrows in technical drawing
To plot the distance arrows in technical drawing in matplotlib we can use annotate() method with arrow properties.
Example
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.axhline(3.5) plt.axhline(2.5) plt.annotate( '''', xy=(0.5, 3.5), xycoords=''data'', xytext=(0.5, 2.5), textcoords=''data'', arrowprops={''arrowstyle'': ''''}) plt.annotate( ''$it{d=1}$'', xy=(0.501, 3.0), xycoords=''data'', xytext=(0.5, 3.5), textcoords=''offset points'') plt.show()
Output
”;