plotting

The plot_* functions behind every .plot() method, and overview.

sonore.plotting

Matplotlib plotting. Every function takes an optional ax and returns it.

Nothing here touches global matplotlib settings; style your notebook however you like (e.g. plt.style.use(...)).

plot_waveform(sound: Sound, ax: Axes | None = None, *, labels: Sequence[str] | None = None, **kwargs) → Axes[source]

The waveform over time, one line per channel. A two-channel sound gets the legend L and R unless labels names the channels; other kwargs go to ax.plot.

plot_spectrum(spectrum: Spectrum, ax: Axes | None = None, *, fscale: Literal['log', 'linear'] = 'log', relative: bool = True, **kwargs) → Axes[source]

The level spectrum in dB against frequency, in dB re its maximum when relative (the default). On the log frequency scale the 0 Hz bin is left out. kwargs go to ax.plot.

plot_stft(stft: STFT, ax: Axes | None = None, *, channel: int = 0, db_range: float = 80.0, cmap: str | Colormap = 'magma', colorbar: bool = True, fmax: float | None = None, trim_edges: bool = True) → Axes[source]

One channel’s spectrogram in dB, the colors spanning the db_range dB below its maximum. With trim_edges (the default) the time windows that reach past either end of the sound are left out: they are zero-padded, which looks like a click. fmax [Hz] limits the frequency axis. Frequency is in Hz when the axis tops out below 10 kHz, in kHz above.

plot_mask(mask: Mask, ax: Axes | None = None, *, channel: int = 0, cmap: str | Colormap = 'Greys_r', colorbar: bool = False, fmax: float | None = None) → Axes[source]

A mask’s gains as an image, black 0 to white 1: time windows by FFT bins for an STFT mask (frequency in Hz or kHz as in plot_stft(), up to fmax [Hz] if given), samples by bands for a subband mask.

plot_modulation_spectrum(ms: ModulationSpectrum, ax: Axes | None = None, *, db_range: float = 60.0, cmap: str | Colormap = 'magma', colorbar: bool = True, wt_max: float | None = None, wf_max: float | None = None) → Axes[source]

The modulation spectrum’s level in dB, temporal modulation [Hz] across and spectral modulation up, the colors spanning the db_range dB below its maximum. wt_max [Hz] and wf_max (in the spectrum’s spectral unit) limit the axes.

plot_modulation_spectrogram(msg: ModulationSpectrogram, ax: Axes | None = None, *, channel: int = 0, band: float | None = None, rate: float | None = None, db_range: float = 30.0, cmap: str = 'magma', colorbar: bool = True) → Axes[source]

Modulation depth in dB as an image; cells the analysis marks invalid (rate above the band’s width, window past either end) are gray. The default view is modulation rate against time, pooled over bands; band= [Hz] picks one acoustic band, rate= [Hz] shows acoustic band against time at one modulation rate. 0 dB is 100% modulation.

plot_modulation_slices(msg: ModulationSpectrogram, t: float, *, rate: float = 4.0, channel: int = 0, db_range: float = 30.0, cmap: str = 'magma', figsize: tuple[float, float] = (13, 3.8)) → Figure[source]

Three linked cuts through a modulation spectrogram’s time x band x rate cube, with a cursor at t [s]: rate against time (pooled over bands), band against time at rate [Hz], and band against rate at t (Atlas and Shamma’s joint display). One color scale for all three. Returns the figure.

animate_modulation_spectrogram(msg: ModulationSpectrogram, *, path: str | os.PathLike | None = None, sound: Sound | None = None, fps: float = 25.0, channel: int = 0, db_range: float = 30.0, cmap: str = 'magma', figsize: tuple[float, float] = (5, 4), dpi: float = 100) → FuncAnimation[source]

The band x rate image moving with time, one video frame every 1/fps seconds. Returns a matplotlib.animation.FuncAnimation (anim.to_jshtml() shows it in a notebook). With path it is saved with ffmpeg; with sound too, the sound is added as the audio track.

plot_subbands(sb: Subbands, axes: Sequence[Axes] | None = None, *, channel: int = 0, sharey: bool = True, color: Any = None, bands: Iterable[int] | None = None) → list[Axes][source]

One trace per band, lowest at the bottom. With sharey=True (default) all traces share one amplitude scale, so relative band levels are visible. When the filterbank has edge filters, the bottom and top traces are its lowpass and highpass (labeled < f_lo and > f_hi).

bands selects which bands to show (indices), e.g. range(1, len(sb) - 1, 5) for every fifth band of a fine filterbank. For an image of all bands, plot the envelopes: sb.envelopes().plot().

plot_envelope(env: TemporalEnvelope, ax: Axes | None = None, *, db: bool = False, **kwargs) → Axes[source]

A single TemporalEnvelope (no filterbank) over time.

plot_envelopes(env: TemporalEnvelope, ax: Axes | None = None, *, channel: int = 0, db_range: float = 40.0, db_max: float | None = None, cmap: str | Colormap = 'magma', colorbar: bool = True, edges: bool = False, align: Literal['peak'] | None = None, fscale: Literal['log', 'linear'] = 'log', fmax: float | None = None) → Axes[source]

A filterbank’s envelopes (a cochleagram) as an image: time x band, in dB, the colors spanning the db_range dB below db_max (default: the image’s own maximum). Give several cochleagrams the same db_max to draw them on one scale, so that equal levels get equal colors. The filterbank’s edge filters, if it has them, are hidden unless edges=True.

align="peak" draws each band earlier by its filter’s envelope_peak_delay (a causal gammatone bank’s latency), so a click is a vertical line; only the drawing moves, not the data. fscale="linear" draws frequency linearly, in Hz or kHz as plot_stft() does, with an optional fmax [Hz].

plot_envelope_peaks(peaks: EnvelopePeaks, ax: Axes | None = None, **kwargs) → Axes[source]

An EnvelopePeaks: for each cutoff, the fraction of bins whose maximum exceeds each value, on log-log axes, with the fitted power law as a dashed line over the values it was fitted to. A power law is a straight line here, with slope minus the exponent.

plot_decimated_subbands(coefs: DecimatedSubbands, ax: Axes | None = None, *, channel: int = 0, db_range: float = 60.0, db_max: float | None = None, cmap: str | Colormap = 'magma', colorbar: bool = True, edges: bool = False) → Axes[source]

A decimated filterbank’s coefficients as an image, each band drawn with its own time cells: long ones for the low bands, which have few coefficients, and short ones for the high bands. Each band’s row reaches halfway (on a log scale) to its neighbors’ centers. Levels are the band envelopes of db, the colors spanning the db_range dB below db_max (default: the largest level inside the sound). The filterbank’s edge filters, if it has them, are hidden unless edges=True.

plot_tf_db(db: np.ndarray, t: ArrayLike, f: ArrayLike, ax: Axes | None = None, *, db_range: float = 60.0, cmap: str | Colormap = 'magma', colorbar: bool = True, fmax: float | None = None, title: str | None = None) → Axes[source]

A time-frequency image from levels db (shape (n_freqs, n_windows)) at window times t [s] and frequencies f [Hz], which need not be uniform: each cell extends halfway to its neighbors. Frequency is linear, in Hz or kHz as in plot_stft().

plot_cepstrum(cep: Cepstrum, ax: Axes | None = None, *, channel: int = 0, q_range: tuple[float, float] = (0.001, 0.015), cmap: str | Colormap = 'magma', colorbar: bool = True) → Axes[source]

A cepstrum as an image: time [s] across, quefrency [ms] up, from q_range [s]. The default, 1 to 15 ms, leaves out the low quefrencies (the spectral envelope, which would set the color scale) and covers the periods of voices from about 67 Hz up. Negative values are drawn as 0, and the color scale tops out at the 99.5th percentile, so that a few isolated peaks do not darken the rest.

plot_mfcc(mfcc: MFCC, ax: Axes | None = None, *, channel: int = 0, kind: Literal['mfcc', 'mel'] = 'mfcc', cmap: str | Colormap | None = None, colorbar: bool = True, db_range: float = 60.0) → Axes[source]

MFCCs as an image, time [s] across.

kind="mfcc": coefficients c1 and up (c0, the level, would set the color scale), on a diverging color scale symmetric about 0 and clipped at the 99th percentile of the magnitudes. kind="mel": the floored mel spectrogram in dB, one row per band, equally spaced so the rows read as the mel scale, labeled with their center frequencies, over db_range dB.

plot_f0_track(track: F0Track, ax: Axes | None = None, *, channel: int = 0, candidates: bool = False, color: Any = 'C0', **kwargs) → Axes[source]

An F0 track: F0 [Hz] against time [s], broken where unvoiced. With candidates=True, every refined candidate the tracker weighed is drawn as a gray dot, darker for a higher periodicity score.

plot_lpc(lpc: LPC, ax: Axes | None = None, *, channel: int = 0, db_range: float = 60.0, fmax: float | None = None, candidates: bool = True, cmap: str | Colormap = 'magma', colorbar: bool = True) → Axes[source]

An LPC model: its envelope in dB as a time-frequency image, with every candidate resonance as a dot, darker for a narrower one.

plot_formant_track(track: FormantTrack, ax: Axes | None = None, *, channel: int = 0, candidates: bool = False, voiced: ArrayLike | None = None, colors: Sequence[Any] = ('C0', 'C1', 'C2'), **kwargs) → Axes[source]

A FormantTrack: F1 to F3 against time [s], each broken where missing or, given voiced (a boolean per time window, such as an F0 track’s voicing on the same grid), where unvoiced. Frequency is in kHz if ax is already labeled so (a spectrogram drawn by plot_stft() up to 10 kHz or more), otherwise in Hz. With candidates=True every resonance the tracker chose from is a gray dot, darker for a narrower one.

plot_descriptor_track(track: DescriptorTrack, ax: Axes | None = None, *, channel: int = 0, **kwargs) → Axes[source]

A DescriptorTrack against time, one channel.

plot_chroma(view: Chroma, ax: Axes | None = None, *, channel: int = 0, db_range: float = 40.0, cmap: str = 'magma') → Axes[source]

A Chroma: the 12 pitch classes, C at the bottom, against time, in dB, the db_range dB below the loudest cell.

plot_interaural_cues(cues: InterauralCues, ax: Axes | None = None, *, show_iac: bool = True) → Axes[source]

Broadband cues: ITD and ILD on twin axes (plus IAC underneath). Per-band cues: ITD as an image, each band labeled by its center frequency.

A time window in which one ear is silent has an infinite ILD and no ITD; it is left out of the lines and the image, and the axis limits come from the other windows.

plot_ripple_pattern(pattern: Pattern, ax: Axes | None = None, *, duration: float = 1.0, f_lo: float = 250.0, f_hi: float = 8000.0, cmap: str | Colormap = 'RdBu_r', colorbar: bool = True, n_t: int | None = None, n_x: int = 200) → Axes[source]

Envelope of a ripple pattern (dB re its mean level) over time and log-frequency, before any sound is made.

plot_lissajous(sound: Sound, ax: Axes | None = None, *, duration: float | None = None, start: float = 0.0, **kwargs) → Axes[source]

A Lissajous figure: the left channel of a two-channel sound against the right, over duration seconds from start (by default the rest of the sound). Two tones whose frequencies are in a small whole-number ratio p:q draw a closed figure that stands still; a slightly mistuned pair draws a figure that turns, passing through all its shapes once every 1 / |q f_left - p f_right| seconds.

overview(sound: Sound, *, win_dur: float = 0.02, figsize: tuple[float, float] = (12, 8), fmax: float | None = None) → Figure[source]

Waveform, spectrum, spectrogram and modulation spectrum in one figure (replaces the old display_STFT). Returns the Figure.

log_frequency_axis(ax: Axes, axis: Literal['x', 'y', 'both'] = 'y') → Axes[source]

Put axis of ax on a log scale labeled in plain numbers (20, 50, 100, 200, 500, 1000, …) instead of powers of ten. The ticks are the densest set that fits the axis’s length: a range of many decades on a small panel gets only 10, 100, 1000, …, and a narrow one gets denser ticks (100, 200, 300, 500; then 800, 1000, 1500, …). Returns ax.