|
| 1 | +""" pyplots.ai |
| 2 | +spectrogram-basic: Spectrogram Time-Frequency Heatmap |
| 3 | +Library: seaborn 0.13.2 | Python 3.13.11 |
| 4 | +Quality: 91/100 | Created: 2025-12-31 |
| 5 | +""" |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | +import seaborn as sns |
| 10 | +from scipy import signal |
| 11 | + |
| 12 | + |
| 13 | +# Data - chirp signal with increasing frequency |
| 14 | +np.random.seed(42) |
| 15 | +sample_rate = 4000 # Hz |
| 16 | +duration = 2.0 # seconds |
| 17 | +t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) |
| 18 | + |
| 19 | +# Create a chirp signal: frequency increases from 100 Hz to 800 Hz |
| 20 | +f0, f1 = 100, 800 |
| 21 | +chirp_signal = signal.chirp(t, f0=f0, f1=f1, t1=duration, method="linear") |
| 22 | + |
| 23 | +# Add some noise for realism |
| 24 | +chirp_signal += np.random.randn(len(chirp_signal)) * 0.1 |
| 25 | + |
| 26 | +# Compute spectrogram using scipy |
| 27 | +nperseg = 256 # Window size |
| 28 | +noverlap = 200 # Overlap for smoother visualization |
| 29 | +frequencies, times, Sxx = signal.spectrogram(chirp_signal, fs=sample_rate, nperseg=nperseg, noverlap=noverlap) |
| 30 | + |
| 31 | +# Convert to dB scale for better visualization |
| 32 | +Sxx_dB = 10 * np.log10(Sxx + 1e-10) |
| 33 | + |
| 34 | +# Create plot |
| 35 | +fig, ax = plt.subplots(figsize=(16, 9)) |
| 36 | + |
| 37 | +# Flip data vertically so low frequencies are at bottom (standard convention) |
| 38 | +Sxx_dB_flipped = np.flipud(Sxx_dB) |
| 39 | + |
| 40 | +# Use seaborn heatmap for the spectrogram visualization |
| 41 | +sns.heatmap( |
| 42 | + Sxx_dB_flipped, |
| 43 | + ax=ax, |
| 44 | + cmap="viridis", |
| 45 | + cbar=True, |
| 46 | + cbar_kws={"label": "Power (dB)", "shrink": 0.8}, |
| 47 | + xticklabels=False, |
| 48 | + yticklabels=False, |
| 49 | + rasterized=True, |
| 50 | +) |
| 51 | + |
| 52 | +# Set proper axis labels and ticks |
| 53 | +# Calculate tick positions for time axis |
| 54 | +time_tick_positions = np.linspace(0, Sxx_dB.shape[1], 5) |
| 55 | +time_tick_labels = [f"{t:.1f}" for t in np.linspace(0, duration, 5)] |
| 56 | +ax.set_xticks(time_tick_positions) |
| 57 | +ax.set_xticklabels(time_tick_labels, fontsize=16) |
| 58 | + |
| 59 | +# Calculate tick positions for frequency axis (low to high, bottom to top) |
| 60 | +freq_tick_positions = np.linspace(0, Sxx_dB.shape[0], 5) |
| 61 | +freq_tick_labels = [f"{int(f)}" for f in np.linspace(frequencies[0], frequencies[-1], 5)] |
| 62 | +ax.set_yticks(freq_tick_positions) |
| 63 | +ax.set_yticklabels(freq_tick_labels[::-1], fontsize=16) |
| 64 | + |
| 65 | +# Labels and styling |
| 66 | +ax.set_xlabel("Time (s)", fontsize=20) |
| 67 | +ax.set_ylabel("Frequency (Hz)", fontsize=20) |
| 68 | +ax.set_title("spectrogram-basic · seaborn · pyplots.ai", fontsize=24, pad=20) |
| 69 | + |
| 70 | +# Adjust colorbar label size |
| 71 | +cbar = ax.collections[0].colorbar |
| 72 | +cbar.ax.tick_params(labelsize=14) |
| 73 | +cbar.ax.yaxis.label.set_size(18) |
| 74 | + |
| 75 | +plt.tight_layout() |
| 76 | +plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
0 commit comments