Stem Plot
Impulse Response Stem
Damped oscillation with envelope curves.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
COLORS = {
'positive': '#6366F1',
'negative': '#EC4899',
'background': '#FFFFFF',
'text': '#1E293B',
'text_muted': '#64748B',
'grid': '#F1F5F9',
}
# Damped oscillation
x = np.arange(0, 25)
y = np.exp(-x/8) * np.cos(x * 0.8) * 10
fig, ax = plt.subplots(figsize=(12, 5), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
for xi, yi in zip(x, y):
color = COLORS['positive'] if yi >= 0 else COLORS['negative']
ax.plot([xi, xi], [0, yi], color=color, linewidth=2, alpha=0.7, zorder=2)
ax.scatter(xi, yi, s=80, c=color, alpha=0.2, zorder=3)
ax.scatter(xi, yi, s=40, c=color, edgecolors='white', linewidths=1.5, zorder=4)
# Envelope
env_x = np.linspace(0, 24, 100)
env_y = np.exp(-env_x/8) * 10
ax.plot(env_x, env_y, color=COLORS['text_muted'], linewidth=1, linestyle='--', alpha=0.5)
ax.plot(env_x, -env_y, color=COLORS['text_muted'], linewidth=1, linestyle='--', alpha=0.5)
ax.axhline(0, color=COLORS['text'], linewidth=1.5, zorder=1)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_visible(False)
ax.yaxis.grid(True, color=COLORS['grid'], linewidth=1, zorder=0)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.set_xlim(-0.5, 25)
ax.set_ylim(-12, 12)
ax.set_xlabel('Sample (n)', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Amplitude', fontsize=10, color=COLORS['text'], labelpad=10)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Basic Charts
More Stem Plot examples
☕