Skip to content
Related Articles

Related Articles

Matplotlib.patches.Arrow Class in Python
  • Last Updated : 21 Apr, 2020

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.

Matplotlib.patches.Arrow

The matplotlib.patches.Arrow class is used to for patching an arrow in the plot. it draws arrow from (x, y) to (x + dx, y + dy) and its width is scaled using the width argument.

Syntax: class matplotlib.patches.Arrow(x, y, dx, dy, width=1.0, **kwargs)

Parameters:

  1. x: It represents the x coordinate of the arrow tail.
  2. y: It represents the y coordinate of the arrow tail.
  3. dx: It represents arrow length in the x direction.
  4. dy: It represents arrow length in the y direction.
  5. width: It is an optional parameter and has default value as 1. it is the scale factor for the width of the arrow. In default value the tail width is 0.2 and the head width is 0.6.
  6. **kwargs: These are the patch properties mentioned below in the table;
PropertyDescription
agg_filtera filter function that takes a (m, n, 3) float array and a dpi value that returns a (m, n, 3) array
alphafloat or None
animatedbool
antialiased or aaunknown
capstyle{‘butt’, ’round’, ‘projecting’}
clip_boxBbox
clip_onbool
clip_path[(Path, Transform)|Patch|None]
colorcolor or sequence of rgba tuples
containscallable
edgecolor or ec or edgecolorscolor or None or ‘auto’
facecolor or fc or facecolorscolor or None
figurefigure
fillbool
gidstr
hatch{‘/’, ‘\’, ‘|’, ‘-‘, ‘+’, ‘x’, ‘o’, ‘O’, ‘.’, ‘*’}
in_layoutbool
joinstyle{‘miter’, ’round’, ‘bevel’}
linestyle or ls{‘-‘, ‘–‘, ‘-.’, ‘:’, ”, (offset, on-off-seq), …}
linewidth or linewidths or lwfloat or None
path_effectsAbstractPathEffect
pickerNone or bool or float or callable
path_effectsAbstractPathEffect
pickerfloat or callable[[Artist, Event], Tuple[bool, dict]]
rasterizedbool or None
sketch_params(scale: float, length: float, randomness: float)
snapbool or None
transformmatplotlib.transforms.Transform
urlstrvisiblebool
zorderfloat

Example 1:






import matplotlib.pyplot as plt
import numpy as np
import matplotlib.path as mpath
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
  
  
def label(xy, text):
      
    # shift y-value for label so that 
    # it's below the artist
    y = xy[1] - 0.15
    plt.text(xy[0], y, text, ha ="center",
             family ='sans-serif', size = 14)
  
  
fig, ax = plt.subplots()
  
# create 3x3 grid to plot 
# the artists
grid = np.mgrid[0.2:0.8:3j
                0.2:0.8:3j].reshape(2, -1).T
  
patches = []
  
  
# add an arrow
arrow = mpatches.Arrow(grid[5, 0] - 0.05
                       grid[5, 1] - 0.05, 0.1, 0.1,
                       width = 0.1)
patches.append(arrow)
label(grid[5], " Sample Arrow")
  
  
colors = np.linspace(0, 1, len(patches))
collection = PatchCollection(patches,
                             cmap = plt.cm.hsv, 
                             alpha = 0.3)
  
collection.set_array(np.array(colors))
ax.add_collection(collection)
  
plt.axis('equal')
plt.axis('off')
plt.tight_layout()
  
plt.show()

Output:

Example 2:




from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle, Arrow
import numpy as np
  
  
nmax = 9
xdata = range(nmax)
ydata = np.random.random(nmax)
  
plt.ion()
fig, ax = plt.subplots()
ax.set_aspect("equal")
ax.plot(xdata, ydata, 'o-')
ax.set_xlim(-1, 10)
ax.set_ylim(-1, 4)
  
  
rect = Rectangle((0, 0), nmax, 1, zorder = 10)
ax.add_patch(rect)
  
x0, y0 = 5, 3
arrow = Arrow(1, 1, x0-1, y0-1, color ="# aa0088")
  
a = ax.add_patch(arrow)
  
plt.draw()
  
for i in range(nmax):
      
    rect.set_x(i)
    rect.set_width(nmax - i)
  
    a.remove()
    arrow = Arrow(1 + i, 1, x0-i + 1, y0-1
                  color ="# aa0088")
    a = ax.add_patch(arrow)
  
    fig.canvas.draw_idle()
    plt.pause(0.4)
  
plt.waitforbuttonpress() 
  
plt.show()

Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :