How to make an arrow that loops in Matplotlib?

To make an arrow that loops in Matplotlib, we can combine a circular arc with an arrowhead. This creates a visual loop with directional indication, useful for representing cyclic processes or feedback loops in diagrams.

Creating the Loop Function

We'll create a custom function that combines a wedge (for the circular part) and a polygon (for the arrow) ?

from matplotlib import pyplot as plt, patches, collections

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

def make_loop(ax, center, radius, theta1=-30, theta2=180):
    # Create the circular arc
    rwidth = 0.02
    ring = patches.Wedge(center, radius, theta1, theta2, width=rwidth)
    
    # Create the arrowhead
    offset = 0.02
    xcent = center[0] - radius + (rwidth / 2)
    left = [xcent - offset, center[1]]
    right = [xcent + offset, center[1]]
    bottom = [(left[0] + right[0]) / 2., center[1] - 0.05]
    arrow = plt.Polygon([left, right, bottom, left])
    
    # Combine ring and arrow into a collection
    p = collections.PatchCollection(
        [ring, arrow],
        edgecolor='orange',
        facecolor='red'
    )
    ax.add_collection(p)

# Create the plot
fig, ax = plt.subplots()
make_loop(ax, center=(.5, .7), radius=.1)

plt.show()

How It Works

The function creates two components:

  • Wedge: A circular arc defined by center, radius, start angle (theta1), and end angle (theta2)
  • Polygon: A triangular arrowhead positioned at the arc's end
  • PatchCollection: Combines both shapes with consistent styling

Customizing the Loop

You can modify the loop's appearance by adjusting parameters ?

from matplotlib import pyplot as plt, patches, collections

def make_custom_loop(ax, center, radius, theta1=-45, theta2=200, colors=('blue', 'darkblue')):
    rwidth = 0.03
    ring = patches.Wedge(center, radius, theta1, theta2, width=rwidth)
    
    # Position arrowhead
    offset = 0.025
    xcent = center[0] - radius + (rwidth / 2)
    left = [xcent - offset, center[1]]
    right = [xcent + offset, center[1]]
    bottom = [(left[0] + right[0]) / 2., center[1] - 0.06]
    arrow = plt.Polygon([left, right, bottom, left])
    
    p = collections.PatchCollection(
        [ring, arrow],
        edgecolor=colors[0],
        facecolor=colors[1]
    )
    ax.add_collection(p)

# Create multiple loops with different styles
fig, ax = plt.subplots()
make_custom_loop(ax, center=(.3, .7), radius=.08, colors=('red', 'darkred'))
make_custom_loop(ax, center=(.7, .7), radius=.08, theta1=-60, theta2=220, colors=('green', 'darkgreen'))

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()

Parameters

Parameter Description Default
center Loop center coordinates (x, y) Required
radius Loop radius Required
theta1 Starting angle in degrees -30
theta2 Ending angle in degrees 180
rwidth Ring width 0.02

Conclusion

Creating loop arrows in Matplotlib involves combining wedge patches with polygon arrowheads. Use PatchCollection to manage both components together with consistent styling. This technique is perfect for flowcharts and process diagrams.

Updated on: 2026-03-25T23:08:53+05:30

218 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements