|
| 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") |
0 commit comments