Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to fill rainbow color under a curve in Python Matplotlib?
Creating a rainbow gradient effect under a curve in Matplotlib involves using the fill_between() function with multiple color layers. This technique creates visually appealing plots by stacking colored regions with slightly different y-offsets.
Basic Rainbow Fill Implementation
The key is to plot multiple fill_between() layers, each with a different color and y-offset ?
import numpy as np
import matplotlib.pyplot as plt
def plot_rainbow_under_curve():
rainbow_colors = ['violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
x = np.linspace(-5, 5, 100)
y = x ** 2
for i in range(len(rainbow_colors)):
plt.fill_between(x, y + (len(rainbow_colors) - i - 1),
color=rainbow_colors[i], alpha=0.8)
plt.plot(x, y, 'black', linewidth=2, label='y = x²')
plt.title('Rainbow Fill Under Curve')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.figure(figsize=(10, 6))
plot_rainbow_under_curve()
plt.show()
Enhanced Version with Smooth Gradient
For a smoother rainbow effect, we can use more colors and adjust transparency ?
import numpy as np
import matplotlib.pyplot as plt
def smooth_rainbow_fill():
# Extended rainbow colors for smoother gradient
colors = ['#9400D3', '#4B0082', '#0000FF', '#00FF00', '#FFFF00', '#FF7F00', '#FF0000']
x = np.linspace(-3, 3, 200)
y = np.sin(x) + 2 # Sine wave shifted up
# Create gradient effect
for i, color in enumerate(colors):
offset = (len(colors) - i - 1) * 0.3
plt.fill_between(x, y + offset, alpha=0.7, color=color)
# Add the main curve
plt.plot(x, y, 'black', linewidth=3, label='y = sin(x) + 2')
plt.title('Smooth Rainbow Gradient Under Sine Wave')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True, alpha=0.3)
plt.legend()
plt.figure(figsize=(12, 8))
smooth_rainbow_fill()
plt.show()
Multiple Curves with Rainbow Fill
You can apply rainbow fills to multiple curves in the same plot ?
import numpy as np
import matplotlib.pyplot as plt
def multiple_rainbow_curves():
rainbow_colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
x = np.linspace(0, 2*np.pi, 100)
curves = [
(np.sin(x) + 3, 'Sine Wave'),
(np.cos(x) + 1, 'Cosine Wave')
]
plt.figure(figsize=(12, 8))
for curve_idx, (y, label) in enumerate(curves):
for i, color in enumerate(rainbow_colors):
offset = (len(rainbow_colors) - i - 1) * 0.2
plt.fill_between(x, y + offset + curve_idx * 0.5,
alpha=0.6, color=color)
plt.plot(x, y, 'black', linewidth=2, label=label)
plt.title('Multiple Curves with Rainbow Fill')
plt.xlabel('x (radians)')
plt.ylabel('y')
plt.legend()
plt.grid(True, alpha=0.3)
multiple_rainbow_curves()
plt.show()
Key Parameters
| Parameter | Purpose | Example Values |
|---|---|---|
alpha |
Transparency level | 0.6, 0.8 |
color |
Fill color | 'red', '#FF0000' |
| offset | Y-axis displacement | 0.2, 0.5 |
Conclusion
Rainbow fills under curves are created by layering multiple fill_between() calls with different colors and y-offsets. Adjust the alpha values and offsets to control the gradient smoothness and visual appeal.
Advertisements
