Plot a rectangle with an edgecolor in Matplotlib

To create a rectangle with an edgecolor in Matplotlib, you need to use the Rectangle class from matplotlib.patches and specify the edgecolor parameter. This allows you to create visually distinct borders around rectangular shapes.

Basic Rectangle with Edge Color

Here's how to create a rectangle with a colored edge ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Create figure and axis
fig, ax = plt.subplots(figsize=(8, 6))

# Create rectangle with edge color
rect = patches.Rectangle((1, 1), 3, 2, 
                        edgecolor='red', 
                        facecolor='lightblue', 
                        linewidth=3)

# Add rectangle to the plot
ax.add_patch(rect)

# Set axis limits and labels
ax.set_xlim(0, 5)
ax.set_ylim(0, 4)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Rectangle with Edge Color')

plt.show()

Rectangle Parameters

The Rectangle constructor accepts several important parameters ?

  • xy − Bottom-left corner coordinates (x, y)

  • width − Width of the rectangle

  • height − Height of the rectangle

  • edgecolor − Color of the rectangle border

  • facecolor − Fill color of the rectangle

  • linewidth − Thickness of the edge

Multiple Rectangles with Different Edge Colors

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig, ax = plt.subplots(figsize=(10, 6))

# Create rectangles with different edge colors
rectangles = [
    patches.Rectangle((0.5, 1), 1.5, 1, edgecolor='red', facecolor='pink', linewidth=2),
    patches.Rectangle((2.5, 1), 1.5, 1, edgecolor='blue', facecolor='lightblue', linewidth=3),
    patches.Rectangle((4.5, 1), 1.5, 1, edgecolor='green', facecolor='lightgreen', linewidth=4)
]

# Add all rectangles to the plot
for rect in rectangles:
    ax.add_patch(rect)

# Add text labels
ax.text(1.25, 1.5, 'Red Edge', ha='center', va='center')
ax.text(3.25, 1.5, 'Blue Edge', ha='center', va='center')
ax.text(5.25, 1.5, 'Green Edge', ha='center', va='center')

# Configure the plot
ax.set_xlim(0, 7)
ax.set_ylim(0, 3)
ax.set_title('Multiple Rectangles with Different Edge Colors')

plt.show()

Advanced Edge Styling

You can also customize the edge style using additional parameters ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig, ax = plt.subplots(figsize=(8, 6))

# Rectangle with dashed edge
rect1 = patches.Rectangle((1, 3), 2, 1, 
                         edgecolor='purple', 
                         facecolor='lavender',
                         linewidth=2,
                         linestyle='--')

# Rectangle with no fill (only edge)
rect2 = patches.Rectangle((4, 3), 2, 1, 
                         edgecolor='orange', 
                         facecolor='none',
                         linewidth=3)

# Rectangle with alpha transparency
rect3 = patches.Rectangle((2.5, 1), 2, 1, 
                         edgecolor='black', 
                         facecolor='yellow',
                         linewidth=2,
                         alpha=0.7)

# Add rectangles to the plot
for rect in [rect1, rect2, rect3]:
    ax.add_patch(rect)

# Add labels
ax.text(2, 3.5, 'Dashed Edge', ha='center', va='center')
ax.text(5, 3.5, 'No Fill', ha='center', va='center')
ax.text(3.5, 1.5, 'Transparent', ha='center', va='center')

ax.set_xlim(0, 7)
ax.set_ylim(0, 5)
ax.set_title('Advanced Rectangle Edge Styling')

plt.show()

Conclusion

Use the edgecolor parameter in matplotlib.patches.Rectangle to create rectangles with colored borders. Combine with linewidth, linestyle, and alpha parameters for advanced edge styling effects.

Updated on: 2026-03-25T20:06:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements