How do I set color to Rectangle in Matplotlib?

In Matplotlib, you can set colors to rectangles using the patches.Rectangle class. This allows you to customize both the edge color and fill color of rectangular shapes in your plots.

Basic Rectangle with Colors

Here's how to create a colored rectangle using edgecolor and facecolor parameters −

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

# Create figure and axis
fig, ax = plt.subplots(figsize=(7, 4))

# Create a rectangle with custom colors
rectangle = patches.Rectangle((1, 1), 3, 2, 
                            edgecolor='orange', 
                            facecolor='lightblue', 
                            linewidth=3)

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

# Set axis limits and labels
ax.set_xlim(0, 5)
ax.set_ylim(0, 4)
ax.set_title('Colored Rectangle in Matplotlib')

plt.show()

Multiple Rectangles with Different Colors

You can create multiple rectangles with different color schemes −

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

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

# Rectangle 1: Red border, yellow fill
rect1 = patches.Rectangle((1, 1), 2, 1.5, 
                         edgecolor='red', 
                         facecolor='yellow', 
                         linewidth=2)

# Rectangle 2: Blue border, green fill
rect2 = patches.Rectangle((4, 2), 2, 2, 
                         edgecolor='blue', 
                         facecolor='lightgreen', 
                         linewidth=2)

# Rectangle 3: Purple border, transparent fill
rect3 = patches.Rectangle((1.5, 3.5), 3, 1, 
                         edgecolor='purple', 
                         facecolor='none', 
                         linewidth=3)

# Add all rectangles
ax.add_patch(rect1)
ax.add_patch(rect2)
ax.add_patch(rect3)

# Set limits and grid
ax.set_xlim(0, 7)
ax.set_ylim(0, 6)
ax.grid(True, alpha=0.3)
ax.set_title('Multiple Colored Rectangles')

plt.show()

Color Options

Matplotlib supports various color formats for rectangles −

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

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

# Different color formats
rectangles = [
    # (x, y, width, height, edge_color, face_color, label)
    (1, 1, 1.5, 1, 'red', 'pink', 'Named Colors'),
    (3, 1, 1.5, 1, '#FF5733', '#FFE5B4', 'Hex Colors'),
    (5, 1, 1.5, 1, (0.2, 0.6, 0.8), (0.8, 0.9, 1.0), 'RGB Tuples'),
    (7, 1, 1.5, 1, 'black', 'none', 'Transparent Fill')
]

for x, y, w, h, edge, face, label in rectangles:
    rect = patches.Rectangle((x, y), w, h, 
                           edgecolor=edge, 
                           facecolor=face, 
                           linewidth=2)
    ax.add_patch(rect)
    ax.text(x + w/2, y - 0.3, label, ha='center', fontsize=8)

ax.set_xlim(0, 9)
ax.set_ylim(0, 3)
ax.set_title('Different Color Format Options')
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Rectangle Parameters

Parameter Description Example Values
edgecolor Border color 'red', '#FF0000', (1,0,0)
facecolor Fill color 'blue', 'none', 'lightblue'
linewidth Border thickness 1, 2, 3.5
alpha Transparency 0.0 to 1.0

Conclusion

Use edgecolor and facecolor parameters in patches.Rectangle() to customize rectangle colors. Matplotlib supports named colors, hex codes, and RGB tuples for flexible color control.

---
Updated on: 2026-03-25T19:58:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements