Sunday, November 17, 2024
Google search engine
HomeLanguagesMatplotlib.patches.Arrow Class in Python

Matplotlib.patches.Arrow Class in Python

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;
Property Description
agg_filter a filter function that takes a (m, n, 3) float array and a dpi value that returns a (m, n, 3) array
alpha float or None
animated bool
antialiased or aa unknown
capstyle {‘butt’, ’round’, ‘projecting’}
clip_box Bbox
clip_on bool
clip_path [(Path, Transform)|Patch|None]
color color or sequence of rgba tuples
contains callable
edgecolor or ec or edgecolors color or None or ‘auto’
facecolor or fc or facecolors color or None
figure figure
fill bool
gid str
hatch {‘/’, ‘\’, ‘|’, ‘-‘, ‘+’, ‘x’, ‘o’, ‘O’, ‘.’, ‘*’}
in_layout bool
joinstyle {‘miter’, ’round’, ‘bevel’}
linestyle or ls {‘-‘, ‘–‘, ‘-.’, ‘:’, ”, (offset, on-off-seq), …}
linewidth or linewidths or lw float or None
path_effects AbstractPathEffect
picker None or bool or float or callable
path_effects AbstractPathEffect
picker float or callable[[Artist, Event], Tuple[bool, dict]]
rasterized bool or None
sketch_params (scale: float, length: float, randomness: float)
snap bool or None
transform matplotlib.transforms.Transform
url str
visible bool
zorder float

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:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments