Skip to content

Commit a7bc4b0

Browse files
feat(seaborn): implement spectrum-basic (#2952)
## Implementation: `spectrum-basic` - seaborn Implements the **seaborn** version of `spectrum-basic`. **File:** `plots/spectrum-basic/implementations/seaborn.py` **Parent Issue:** #2926 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20612813037)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent faea52a commit a7bc4b0

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
""" pyplots.ai
2+
spectrum-basic: Frequency Spectrum Plot
3+
Library: seaborn 0.13.2 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-31
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
import seaborn as sns
10+
11+
12+
# Data - Create a synthetic signal with multiple frequency components
13+
np.random.seed(42)
14+
15+
# Sampling parameters
16+
sample_rate = 1000 # Hz
17+
duration = 1.0 # seconds
18+
n_samples = int(sample_rate * duration)
19+
t = np.linspace(0, duration, n_samples, endpoint=False)
20+
21+
# Create signal with multiple frequency components (simulating machinery vibration)
22+
# Fundamental frequency at 50 Hz, harmonics at 100 Hz and 150 Hz, plus some noise
23+
signal = (
24+
2.0 * np.sin(2 * np.pi * 50 * t) # 50 Hz fundamental
25+
+ 1.2 * np.sin(2 * np.pi * 100 * t) # 100 Hz harmonic
26+
+ 0.8 * np.sin(2 * np.pi * 150 * t) # 150 Hz harmonic
27+
+ 0.3 * np.sin(2 * np.pi * 220 * t) # 220 Hz component
28+
+ 0.4 * np.random.randn(n_samples) # noise
29+
)
30+
31+
# Compute FFT
32+
fft_result = np.fft.fft(signal)
33+
frequencies = np.fft.fftfreq(n_samples, 1 / sample_rate)
34+
35+
# Take only positive frequencies
36+
positive_mask = frequencies >= 0
37+
frequencies = frequencies[positive_mask]
38+
amplitude = np.abs(fft_result[positive_mask]) * 2 / n_samples # Normalize amplitude
39+
40+
# Convert to dB scale for better visualization
41+
amplitude_db = 20 * np.log10(amplitude + 1e-10) # Add small value to avoid log(0)
42+
43+
# Plot
44+
sns.set_context("talk", font_scale=1.2)
45+
fig, ax = plt.subplots(figsize=(16, 9))
46+
47+
# Use seaborn lineplot for the spectrum
48+
sns.lineplot(x=frequencies, y=amplitude_db, ax=ax, color="#306998", linewidth=2.5)
49+
50+
# Fill under the curve for better visualization
51+
ax.fill_between(frequencies, amplitude_db, alpha=0.3, color="#306998")
52+
53+
# Mark peak frequencies
54+
peak_indices = np.where((amplitude_db > -20) & (frequencies > 10))[0]
55+
for idx in peak_indices:
56+
if amplitude_db[idx] > amplitude_db[max(0, idx - 5) : min(len(amplitude_db), idx + 6)].mean() + 5:
57+
ax.axvline(x=frequencies[idx], color="#FFD43B", alpha=0.5, linestyle="--", linewidth=1.5)
58+
59+
# Styling
60+
ax.set_xlabel("Frequency (Hz)", fontsize=20)
61+
ax.set_ylabel("Amplitude (dB)", fontsize=20)
62+
ax.set_title("spectrum-basic · seaborn · pyplots.ai", fontsize=24)
63+
ax.tick_params(axis="both", labelsize=16)
64+
ax.set_xlim(0, 300) # Focus on the frequency range of interest
65+
ax.set_ylim(-60, 10)
66+
ax.grid(True, alpha=0.3, linestyle="--")
67+
68+
plt.tight_layout()
69+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library: seaborn
2+
specification_id: spectrum-basic
3+
created: '2025-12-31T05:34:05Z'
4+
updated: '2025-12-31T05:45:42Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20612813037
7+
issue: 2926
8+
python_version: 3.13.11
9+
library_version: 0.13.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/spectrum-basic/seaborn/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/spectrum-basic/seaborn/plot_thumb.png
12+
preview_html: null
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent visual clarity with properly sized text elements and good color contrast
17+
- Realistic machinery vibration scenario with fundamental frequency and harmonics
18+
- Effective use of fill_between to enhance the spectrum visualization
19+
- Peak frequency highlighting with vertical dashed lines aids interpretation
20+
- Proper use of dB scale as recommended in the specification
21+
- Clean KISS code structure with reproducible random seed
22+
weaknesses:
23+
- Uses seaborn primarily for lineplot which is relatively basic; could leverage
24+
more distinctive seaborn features
25+
- Grid styling uses alpha=0.3 which is acceptable but slightly at the edge of the
26+
0.2-0.4 recommendation

0 commit comments

Comments
 (0)