Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions plots/spectrum-basic/implementations/python/plotly.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
""" pyplots.ai
""" anyplot.ai
spectrum-basic: Frequency Spectrum Plot
Library: plotly 6.5.0 | Python 3.13.11
Quality: 93/100 | Created: 2025-12-31
Library: plotly 6.7.0 | Python 3.13.13
Quality: 93/100 | Updated: 2026-05-14
"""

import os

import numpy as np
import plotly.graph_objects as go


# Theme tokens
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
GRID = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)"
BRAND = "#009E73"

# Data - Generate synthetic signal with multiple frequency components
np.random.seed(42)

Expand All @@ -28,7 +39,7 @@
# Compute FFT
fft_result = np.fft.rfft(signal)
frequency = np.fft.rfftfreq(n_samples, 1 / sample_rate)
amplitude_db = 20 * np.log10(np.abs(fft_result) / n_samples + 1e-10) # Convert to dB
amplitude_db = 20 * np.log10(np.abs(fft_result) / n_samples + 1e-10)

# Plot
fig = go.Figure()
Expand All @@ -38,32 +49,40 @@
x=frequency,
y=amplitude_db,
mode="lines",
line=dict(color="#306998", width=2),
line=dict(color=BRAND, width=3),
fill="tozeroy",
fillcolor="rgba(48, 105, 152, 0.3)",
fillcolor="rgba(0, 158, 115, 0.15)",
name="Spectrum",
hovertemplate="<b>Frequency:</b> %{x:.1f} Hz<br><b>Amplitude:</b> %{y:.1f} dB<extra></extra>",
)
)

# Layout
# Layout with theme-adaptive colors
fig.update_layout(
title=dict(text="spectrum-basic · plotly · pyplots.ai", font=dict(size=28), x=0.5, xanchor="center"),
title=dict(text="spectrum-basic · plotly · anyplot.ai", font=dict(size=28, color=INK), x=0.5, xanchor="center"),
xaxis=dict(
title=dict(text="Frequency (Hz)", font=dict(size=22)),
tickfont=dict(size=18),
title=dict(text="Frequency (Hz)", font=dict(size=22, color=INK)),
tickfont=dict(size=18, color=INK_SOFT),
range=[0, 500],
gridcolor="rgba(128, 128, 128, 0.3)",
gridwidth=1,
gridcolor=GRID,
gridwidth=0.8,
linecolor=INK_SOFT,
zerolinecolor=INK_SOFT,
),
yaxis=dict(
title=dict(text="Amplitude (dB)", font=dict(size=22)),
tickfont=dict(size=18),
gridcolor="rgba(128, 128, 128, 0.3)",
gridwidth=1,
title=dict(text="Amplitude (dB)", font=dict(size=22, color=INK)),
tickfont=dict(size=18, color=INK_SOFT),
gridcolor=GRID,
gridwidth=0.8,
linecolor=INK_SOFT,
zerolinecolor=INK_SOFT,
),
template="plotly_white",
paper_bgcolor=PAGE_BG,
plot_bgcolor=PAGE_BG,
font=dict(color=INK),
showlegend=False,
margin=dict(l=80, r=40, t=80, b=60),
hovermode="x unified",
margin=dict(l=100, r=60, t=100, b=80),
)

# Add annotations for peak frequencies
Expand All @@ -76,14 +95,14 @@
text=f"{freq} Hz",
showarrow=True,
arrowhead=2,
arrowsize=1,
arrowsize=1.5,
arrowwidth=2,
arrowcolor="#FFD43B",
font=dict(size=16, color="#306998"),
arrowcolor=INK_SOFT,
font=dict(size=16, color=INK),
ax=0,
ay=-40,
ay=-50,
)

# Save
fig.write_image("plot.png", width=1600, height=900, scale=3)
fig.write_html("plot.html")
fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3)
fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn")
Loading
Loading