Skip to content

Commit 0d89044

Browse files
feat(seaborn): implement spectrogram-basic (#2954)
## Implementation: `spectrogram-basic` - seaborn Implements the **seaborn** version of `spectrogram-basic`. **File:** `plots/spectrogram-basic/implementations/seaborn.py` **Parent Issue:** #2927 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20612803772)* --------- 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 bff1016 commit 0d89044

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library: seaborn
2+
specification_id: spectrogram-basic
3+
created: '2025-12-31T05:34:16Z'
4+
updated: '2025-12-31T05:45:53Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20612803772
7+
issue: 2927
8+
python_version: 3.13.11
9+
library_version: 0.13.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/spectrogram-basic/seaborn/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/spectrogram-basic/seaborn/plot_thumb.png
12+
preview_html: null
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent visualization of chirp signal showing clear time-frequency relationship
17+
with bright diagonal trace
18+
- Proper use of perceptually uniform viridis colormap as recommended in specification
19+
- Well-formatted colorbar with dB units and appropriate sizing
20+
- All text elements properly sized for the 16:9 aspect ratio output
21+
- Correct use of seaborn heatmap for spectrogram visualization with proper axis
22+
orientation
23+
weaknesses:
24+
- No grid lines present (minor issue for heatmap plots)

0 commit comments

Comments
 (0)