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
69 changes: 69 additions & 0 deletions plots/spectrum-basic/implementations/seaborn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
""" pyplots.ai
spectrum-basic: Frequency Spectrum Plot
Library: seaborn 0.13.2 | Python 3.13.11
Quality: 92/100 | Created: 2025-12-31
"""

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns


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

# Sampling parameters
sample_rate = 1000 # Hz
duration = 1.0 # seconds
n_samples = int(sample_rate * duration)
t = np.linspace(0, duration, n_samples, endpoint=False)

# Create signal with multiple frequency components (simulating machinery vibration)
# Fundamental frequency at 50 Hz, harmonics at 100 Hz and 150 Hz, plus some noise
signal = (
2.0 * np.sin(2 * np.pi * 50 * t) # 50 Hz fundamental
+ 1.2 * np.sin(2 * np.pi * 100 * t) # 100 Hz harmonic
+ 0.8 * np.sin(2 * np.pi * 150 * t) # 150 Hz harmonic
+ 0.3 * np.sin(2 * np.pi * 220 * t) # 220 Hz component
+ 0.4 * np.random.randn(n_samples) # noise
)

# Compute FFT
fft_result = np.fft.fft(signal)
frequencies = np.fft.fftfreq(n_samples, 1 / sample_rate)

# Take only positive frequencies
positive_mask = frequencies >= 0
frequencies = frequencies[positive_mask]
amplitude = np.abs(fft_result[positive_mask]) * 2 / n_samples # Normalize amplitude

# Convert to dB scale for better visualization
amplitude_db = 20 * np.log10(amplitude + 1e-10) # Add small value to avoid log(0)

# Plot
sns.set_context("talk", font_scale=1.2)
fig, ax = plt.subplots(figsize=(16, 9))

# Use seaborn lineplot for the spectrum
sns.lineplot(x=frequencies, y=amplitude_db, ax=ax, color="#306998", linewidth=2.5)

# Fill under the curve for better visualization
ax.fill_between(frequencies, amplitude_db, alpha=0.3, color="#306998")

# Mark peak frequencies
peak_indices = np.where((amplitude_db > -20) & (frequencies > 10))[0]
for idx in peak_indices:
if amplitude_db[idx] > amplitude_db[max(0, idx - 5) : min(len(amplitude_db), idx + 6)].mean() + 5:
ax.axvline(x=frequencies[idx], color="#FFD43B", alpha=0.5, linestyle="--", linewidth=1.5)

# Styling
ax.set_xlabel("Frequency (Hz)", fontsize=20)
ax.set_ylabel("Amplitude (dB)", fontsize=20)
ax.set_title("spectrum-basic · seaborn · pyplots.ai", fontsize=24)
ax.tick_params(axis="both", labelsize=16)
ax.set_xlim(0, 300) # Focus on the frequency range of interest
ax.set_ylim(-60, 10)
ax.grid(True, alpha=0.3, linestyle="--")

plt.tight_layout()
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
26 changes: 26 additions & 0 deletions plots/spectrum-basic/metadata/seaborn.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
library: seaborn
specification_id: spectrum-basic
created: '2025-12-31T05:34:05Z'
updated: '2025-12-31T05:45:42Z'
generated_by: claude-opus-4-5-20251101
workflow_run: 20612813037
issue: 2926
python_version: 3.13.11
library_version: 0.13.2
preview_url: https://storage.googleapis.com/pyplots-images/plots/spectrum-basic/seaborn/plot.png
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/spectrum-basic/seaborn/plot_thumb.png
preview_html: null
quality_score: 92
review:
strengths:
- Excellent visual clarity with properly sized text elements and good color contrast
- Realistic machinery vibration scenario with fundamental frequency and harmonics
- Effective use of fill_between to enhance the spectrum visualization
- Peak frequency highlighting with vertical dashed lines aids interpretation
- Proper use of dB scale as recommended in the specification
- Clean KISS code structure with reproducible random seed
weaknesses:
- Uses seaborn primarily for lineplot which is relatively basic; could leverage
more distinctive seaborn features
- Grid styling uses alpha=0.3 which is acceptable but slightly at the edge of the
0.2-0.4 recommendation