向 Librosa Waveplot 添加标记 Python

Add marker to Librosa Waveplot Python

我一直在关注 Librosa 文档中的教程和示例。我已经设法创建了一个波图,但我无法弄清楚波图和 matplotlib 之间的相关性。但是我尝试添加一个标记,它不起作用。我只是收到各种不同的错误。如果有人知道该怎么做,我将不胜感激?

顺便说一句:任何 mp3 都可以用来测试这段代码

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')

据我所知,Libra.display.waveplot() 没有设置标记的功能。我对声音波形一无所知。我不知道我想要什么输出,但 matplotlib 似乎可以处理它。

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()