The overlapping transparency issue occurs because matplotlib's default blending mode adds the alpha values together. When two semi-transparent areas overlap, they become more opaque.
Here are several solutions:
Option 1: Use alpha_composite blending
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
# Your line plots here
ax.fill_between(x, y1_lower, y1_upper, alpha=0.15, color='blue')
ax.fill_between(x, y2_lower, y2_upper, alpha=0.15, color='red')
ax.fill_between(x, y3_lower, y3_upper, alpha=0.15, color='green')
# Set the blending mode
for collection in ax.collections:
collection.set_rasterized(True)
# Use a different backend or adjust the rendering
plt.rcParams['path.effects'] = []
Option 2: Create a single composite errorbar
Instead of overlapping fills, create one combined errorbar region:
# Combine your error bounds
combined_lower = np.minimum.reduce([y1_lower, y2_lower, y3_lower])
combined_upper = np.maximum.reduce([y1_upper, y2_upper, y3_upper])
ax.fill_between(x, combined_lower, combined_upper, alpha=0.15, color='gray')
# Then plot your lines on top
Option 3: Use separate subplots or offset the data
# Slightly offset your x-values
x_offset1 = x - 0.01
x_offset2 = x
x_offset3 = x + 0.01
ax.fill_between(x_offset1, y1_lower, y1_upper, alpha=0.15, color='blue')
ax.fill_between(x_offset2, y2_lower, y2_upper, alpha=0.15, color='red')
ax.fill_between(x_offset3, y3_lower, y3_upper, alpha=0.15, color='green')
Option 4: Use zorder with careful alpha management
ax.fill_between(x, y1_lower, y1_upper, alpha=0.15, color='blue', zorder=1)
ax.fill_between(x, y2_lower, y2_upper, alpha=0.15, color='red', zorder=2)
ax.fill_between(x, y3_lower, y3_upper, alpha=0.15, color='green', zorder=3)
The most effective approach is usually Option 1 or 2, depending on whether you want to show individual error regions or a combined uncertainty area.