Chris Parmer — home

Stem plot of damped sinusoid

DSP

Example from the compendium of canonical charts

DSP — Stem plot of damped sinusoid

Python Code

"""DSP — Stem plot of damped sinusoid."""
from pathlib import Path

# ── Palette + theme (matching the Plotly Studio gallery these charts ship in) ──
VIOLET, TEAL, GREEN, PINK, ORANGE = "#845EEE", "#52B3D0", "#55B685", "#DA5597", "#E9A23B"
PRIMARY, SECONDARY = VIOLET, TEAL
COLORWAY = [VIOLET, TEAL, GREEN, PINK, ORANGE]
BG, TEXT, GRID, MUTED = "#ffffff", "#1c2024", "#d9d9e0", "#60646c"
FONT = "Inter, -apple-system, BlinkMacSystemFont, sans-serif"
COLORSCALE = [[0, "rgba(132, 94, 238, 0.05)"], [1, "rgba(132, 94, 238, 0.9)"]]


def apply_theme(fig):
    """Light gallery theme: white background, Inter font, soft gridlines."""
    fig.update_layout(
        paper_bgcolor=BG, plot_bgcolor=BG, colorway=COLORWAY,
        font=dict(family=FONT, color=TEXT, size=12),
        legend=dict(font=dict(color=TEXT)),
        hoverlabel=dict(bgcolor="#f0f0f3", font=dict(color=TEXT, family=FONT), bordercolor=GRID),
    )
    fig.update_xaxes(gridcolor=GRID, linecolor=GRID, zerolinecolor=GRID)
    fig.update_yaxes(gridcolor=GRID, linecolor=GRID, zerolinecolor=GRID)


def fetch_csv(url, **kwargs):
    import io
    import pandas as pd
    import requests
    r = requests.get(url, timeout=60)
    r.raise_for_status()
    return pd.read_csv(io.StringIO(r.text), **kwargs)


def fetch_json(url):
    import requests
    r = requests.get(url, timeout=60)
    r.raise_for_status()
    return r.json()

import numpy as np
import plotly.graph_objects as go


def generate():
    n = np.arange(50)
    y = np.exp(-0.1 * n) * np.sin(2 * np.pi * n / 10)

    # Build None-separated stems for efficiency
    x_stems = []
    y_stems = []
    for i, yi in zip(n, y):
        x_stems += [i, i, None]
        y_stems += [0, yi, None]

    fig = go.Figure()

    # Baseline
    fig.add_trace(go.Scatter(
        x=[0, 49], y=[0, 0],
        mode="lines",
        line=dict(color=GRID, width=1, dash="dot"),
        showlegend=False,
        hoverinfo="skip",
    ))

    # Stems
    fig.add_trace(go.Scatter(
        x=x_stems, y=y_stems,
        mode="lines",
        name="Stems",
        line=dict(color=VIOLET, width=1.5),
        hoverinfo="skip",
        showlegend=False,
    ))

    # Lollipop heads
    fig.add_trace(go.Scatter(
        x=n, y=y,
        mode="markers",
        name="x[n]",
        marker=dict(size=8, color=VIOLET),
        hovertemplate="n=%{x}, x[n]=%{y:.4f}<extra></extra>",
    ))

    fig.update_layout(
        xaxis=dict(title="n (samples)"),
        yaxis=dict(title="Amplitude"),
        legend=dict(orientation="h", y=1.08),
        margin=dict(t=40, b=60, l=70, r=40),
        height=460,
    )
    apply_theme(fig)
    return fig


fig = generate()
fig.show()

Made with Plotly