view auditok/plotting.py @ 249:a9dc1a5115a7

Add plotting.py
author Amine Sehili <amine.sehili@gmail.com>
date Mon, 26 Aug 2019 20:55:16 +0200
parents
children a9f4adfae459
line wrap: on
line source
import matplotlib.pyplot as plt
import numpy as np


def plot(data, sampling_rate, show=True):
    y = np.asarray(data)
    ymax = np.abs(y).max()
    nb_samples = y.shape[-1]
    sample_duration = 1 / sampling_rate
    x = np.linspace(0, sample_duration * (nb_samples - 1), nb_samples)
    plt.plot(x, y / ymax, c="#024959")
    plt.ylim(-3, 3)
    if show:
        plt.show()


def plot_detections(data, sampling_rate, detections, show=True, save_as=None):

    plot(data, sampling_rate, show=False)
    if detections is not None:
        for (start, end) in detections:
            plt.axvspan(start, end, facecolor="g", ec="r", lw=2, alpha=0.4)

    if save_as is not None:
        plt.savefig(save_as, dpi=120)

    if show:
        plt.show()
    return