Line & Scatter

Gradient Area Fill

Area chart with smooth vertical gradient fill effect.

Output
Gradient Area Fill
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# === STYLE CONFIG ===
COLORS = {
    'line': '#8B5CF6',       # Violet
    'gradient_top': '#8B5CF6',
    'gradient_bottom': '#0F172A',
    'background': '#0F172A',
    'text': '#E2E8F0',
    'grid': '#334155',
}

# === DATA ===
x = np.linspace(0, 10, 200)
y = 3 + 2 * np.sin(x) + np.sin(2 * x)

# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

# === GRADIENT FILL ===
# Create gradient image
gradient = np.linspace(0, 1, 256).reshape(-1, 1)
gradient = np.hstack([gradient, gradient])

# Custom colormap
cmap = LinearSegmentedColormap.from_list('custom', 
    [(0, COLORS['gradient_bottom']), (1, COLORS['gradient_top'])])

# Plot gradient as image, masked by the area
ax.imshow(gradient, extent=[0, 10, 0, max(y)*1.1], aspect='auto', 
          cmap=cmap, alpha=0.6, origin='lower')

# Mask above the line
ax.fill_between(x, y, max(y)*1.1, color=COLORS['background'])

# Main line with glow
for lw, alpha in [(6, 0.1), (4, 0.2), (2, 1.0)]:
    ax.plot(x, y, color=COLORS['line'], linewidth=lw, alpha=alpha)

# === AXES ===
ax.set_xlim(0, 10)
ax.set_ylim(0, 7)
ax.set_xlabel('Time', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Value', fontsize=10, color=COLORS['text'], labelpad=10)

# === STYLING ===
for spine in ax.spines.values():
    spine.set_visible(False)

ax.yaxis.grid(True, color=COLORS['grid'], linewidth=0.5, alpha=0.3)
ax.xaxis.grid(False)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text'], labelsize=9, length=0, pad=8)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support