How to set different opacity of edgecolor and facecolor of a patch in Matplotlib?

In Matplotlib, you can set different opacity levels for edgecolor and facecolor of patches by using RGBA color tuples, where the fourth value (alpha) controls transparency. This allows you to create visually appealing graphics with varying opacity levels.

Understanding RGBA Color Format

RGBA color format uses four values: Red, Green, Blue, and Alpha (opacity). The alpha value ranges from 0 (completely transparent) to 1 (completely opaque).

Basic Example

Here's how to create a rectangle patch with different opacity for edge and face colors ?

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

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

# Create figure and axis
figure, ax = plt.subplots()

# Set different opacity values
edge_color_opacity = 1.0      # Fully opaque edge
face_color_opacity = 0.3      # Semi-transparent face

# Add rectangle patch with different opacities
ax.add_patch(patches.Rectangle((0.25, 0.25), 0.50, 0.50,
                              edgecolor=(1, 0, 0, edge_color_opacity),  # Red edge, fully opaque
                              facecolor=(0, 1, 0, face_color_opacity), # Green face, 30% opacity
                              linewidth=3))

# Set axis limits and show
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.show()

Multiple Patches with Varying Opacity

You can create multiple patches with different opacity combinations ?

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

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

# Create patches with different opacity combinations
patches_data = [
    {'pos': (0.1, 0.7), 'edge_alpha': 1.0, 'face_alpha': 0.2, 'edge_color': (1, 0, 0), 'face_color': (0, 0, 1)},
    {'pos': (0.4, 0.7), 'edge_alpha': 0.5, 'face_alpha': 0.8, 'edge_color': (0, 1, 0), 'face_color': (1, 0, 1)},
    {'pos': (0.7, 0.7), 'edge_alpha': 0.3, 'face_alpha': 1.0, 'edge_color': (1, 1, 0), 'face_color': (0, 1, 1)},
]

for patch_data in patches_data:
    edge_rgba = patch_data['edge_color'] + (patch_data['edge_alpha'],)
    face_rgba = patch_data['face_color'] + (patch_data['face_alpha'],)
    
    rect = patches.Rectangle(patch_data['pos'], 0.2, 0.2,
                           edgecolor=edge_rgba,
                           facecolor=face_rgba,
                           linewidth=2)
    ax.add_patch(rect)

ax.set_xlim(0, 1)
ax.set_ylim(0.5, 1)
ax.set_title('Patches with Different Edge and Face Opacity')
plt.show()

Using Named Colors with Alpha

You can also use named colors with separate alpha parameters ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as mcolors

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

# Convert named colors to RGBA with custom alpha
edge_color = mcolors.to_rgba('red', alpha=0.8)
face_color = mcolors.to_rgba('blue', alpha=0.4)

# Create a circle patch
circle = patches.Circle((0.5, 0.5), 0.3,
                       edgecolor=edge_color,
                       facecolor=face_color,
                       linewidth=4)

ax.add_patch(circle)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal')
ax.set_title('Circle with Named Colors and Custom Alpha')
plt.show()

Comparison of Opacity Methods

Method Format Example Best For
RGBA Tuple (R, G, B, A) (1, 0, 0, 0.5) Direct control
Named Color + Alpha to_rgba(color, alpha) to_rgba('red', 0.5) Readable code
Hex + Alpha Separate alpha param color='#FF0000', alpha=0.5 Web colors

Conclusion

Use RGBA color tuples to set different opacity levels for edgecolor and facecolor in Matplotlib patches. The alpha value (fourth component) controls transparency, allowing you to create layered visual effects with varying opacity levels.

---
Updated on: 2026-03-25T22:36:19+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements