Search code examples
pythonmatplotlibmarkerlibrosa

Add marker to Librosa Waveplot Python


I've been following tutorials and examples from the Librosa documentation. I've managed to create a waveplot, but I can't figure out the correlation between waveplot and matplotlib. However I try to add a marker, it doesn't work. I just get a variety of different errors. I would really appreciate if someone knows how to do this?

BTW: Any mp3 can be used to test this code

import librosa.display
import matplotlib.pyplot as plt

filename = 'shut_up_and_dance.mp3'


def main():
    # Load the mp3 file and define variables
    y, sr = librosa.load(filename, duration=10)
    fig, ax = plt.subplots(nrows=1, sharex=True, sharey=True)
    

    y_harm, y_perc = librosa.effects.hpss(y)
    librosa.display.waveplot(y_harm, sr=sr, alpha=0.25, ax=ax)
    librosa.display.waveplot(y_perc, sr=sr, color='r', alpha=0.5, ax=ax)
    ax.set(title='Harmonic + Percussive')
    plt.show()


if __name__ == '__main__':
    main()

print('end program')

Solution

  • The Libra.display.waveplot() does not, as far as I can tell, have the ability to set markers. I have no knowledge of sound waveforms. I don't know what output I want, but matplotlib seems to be able to handle it.

    import librosa.display
    import matplotlib.pyplot as plt
    import numpy as np
    
    filename = '/content/test_music.mp3'
    
    def main():
        # Load the mp3 file and define variables
        y, sr = librosa.load(filename, duration=10)
        fig, ax = plt.subplots(nrows=1, sharex=True, sharey=True, figsize=(16,9))
    
        y_harm, y_perc = librosa.effects.hpss(y)
        librosa.display.waveplot(y_harm, sr=sr, alpha=0.25, ax=ax)
        librosa.display.waveplot(y_perc, sr=sr, color='r', alpha=0.5, ax=ax)
        ax.set(title='Harmonic + Percussive')
        ax.plot(np.arange(1,220501), y_harm, marker='o', markersize=10)
        plt.show()
    
    
    if __name__ == '__main__':
        main()
    

    enter image description here