Line & Scatter
Spectrum Line
Line with rainbow gradient color along its path.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import LineCollection
# === STYLE CONFIG ===
COLORS = {
'background': '#0F172A',
'text': '#E2E8F0',
'grid': '#334155',
}
# === DATA ===
x = np.linspace(0, 10, 300)
y = 4 + 2 * np.sin(x) + 0.5 * np.sin(3 * x)
# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
# === RAINBOW LINE ===
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
# Color based on x position
colors = plt.cm.rainbow(np.linspace(0, 1, len(segments)))
# Glow layers
for lw, alpha in [(12, 0.08), (8, 0.12), (5, 0.2)]:
lc = LineCollection(segments, colors=colors, linewidth=lw, alpha=alpha)
ax.add_collection(lc)
# Main line
lc = LineCollection(segments, colors=colors, linewidth=2.5)
ax.add_collection(lc)
# === AXES ===
ax.set_xlim(0, 10)
ax.set_ylim(0, 8)
ax.set_xlabel('X', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Y', 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.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
More Line & Scatter examples
☕