{"spec_id":"spectrum-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nspectrum-basic: Frequency Spectrum Plot\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-14\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# Theme tokens (see prompts/default-style-guide.md)\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1\n\n# Data: Generate synthetic signal with multiple frequency components\nnp.random.seed(42)\nsample_rate = 8192  # Hz\nduration = 1.0  # seconds\nn_samples = int(sample_rate * duration)\nt = np.linspace(0, duration, n_samples, endpoint=False)\n\n# Create composite signal: 50 Hz base, 150 Hz harmonic, 400 Hz component, plus noise\nsignal = (\n    1.0 * np.sin(2 * np.pi * 50 * t)  # Fundamental at 50 Hz\n    + 0.5 * np.sin(2 * np.pi * 150 * t)  # Harmonic at 150 Hz\n    + 0.3 * np.sin(2 * np.pi * 400 * t)  # Component at 400 Hz\n    + 0.1 * np.random.randn(n_samples)  # Noise\n)\n\n# Compute FFT\nfft_result = np.fft.rfft(signal)\nfrequencies = np.fft.rfftfreq(n_samples, 1 / sample_rate)\namplitude = np.abs(fft_result) / n_samples\n\n# Convert to dB scale (with floor to avoid log(0))\namplitude_db = 20 * np.log10(np.maximum(amplitude, 1e-10))\n\n# Limit to 500 Hz for better visualization\nmask = frequencies <= 500\nfrequencies = frequencies[mask]\namplitude_db = amplitude_db[mask]\n\n# Create data source with formatted strings for hover\nsource = ColumnDataSource(\n    data={\n        \"frequency\": frequencies,\n        \"amplitude\": amplitude_db,\n        \"freq_str\": [f\"{f:.1f} Hz\" for f in frequencies],\n        \"amp_str\": [f\"{a:.1f} dB\" for a in amplitude_db],\n    }\n)\n\n# Create figure (4800 x 2700 px for 16:9)\np = figure(\n    width=4800,\n    height=2700,\n    title=\"spectrum-basic · bokeh · anyplot.ai\",\n    x_axis_label=\"Frequency (Hz)\",\n    y_axis_label=\"Amplitude (dB)\",\n    tools=\"pan,wheel_zoom,box_zoom,reset,save\",\n)\n\n# Plot spectrum as line\np.line(x=\"frequency\", y=\"amplitude\", source=source, line_width=4, line_color=BRAND, legend_label=\"Signal Spectrum\")\n\n# Add subtle fill under the curve\np.varea(x=\"frequency\", y1=\"amplitude\", y2=-80, source=source, fill_color=BRAND, fill_alpha=0.15)\n\n# Add HoverTool for interactivity\nhover = HoverTool(tooltips=[(\"Frequency\", \"@freq_str\"), (\"Amplitude\", \"@amp_str\")])\np.add_tools(hover)\n\n# Mark peak frequencies with circles\npeak_freqs = [50, 150, 400]\npeak_colors = [\"#C475FD\", \"#4467A3\", \"#BD8233\"]  # Okabe-Ito positions 2, 3, 4\nfor freq, color in zip(peak_freqs, peak_colors, strict=True):\n    freq_idx = np.argmin(np.abs(frequencies - freq))\n    peak_amp = amplitude_db[freq_idx]\n\n    p.scatter(\n        x=[freq], y=[peak_amp], size=25, color=color, line_color=INK_SOFT, line_width=2, legend_label=f\"Peak: {freq} Hz\"\n    )\n\n# Styling - text sizes for large canvas (4800x2700)\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\n# Axis lines and ticks\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\n# Grid styling - subtle\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\n\n# Legend styling with larger text\nif p.legend:\n    p.legend.label_text_font_size = \"18pt\"\n    p.legend.label_text_color = INK_SOFT\n    p.legend.location = \"top_right\"\n    p.legend.background_fill_color = ELEVATED_BG\n    p.legend.border_line_color = INK_SOFT\n    p.legend.padding = 15\n    p.legend.spacing = 8\n\n# Background and border\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Save the interactive HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome — Selenium 4 / Selenium Manager\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}