0

我正在尝试制作歌曲的波形。并将其显示在由 PyQt 包装的 matplotlib 窗口中,但我一直遇到错误:

 RuntimeError: Can not put single artist in more than one figure

这发生在我尝试这样做时:

self.axes.add_collection(col)

与我导入 pyplot 时不同,它不会在调用时自行添加到绘图中waveform。所以这就是问题所在,使用 add_collection,我可以用谷歌搜索的唯一方法对我来说效果不佳。

附加信息,col是一个PolyCollection.

这是代码完整代码:

import sys
from PyQt5.QtWidgets import *
import librosa.display
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import os
import random


class WaveForm(FigureCanvas):
    def __init__(self, parent=None):

        dir_path = 'D:\\Musikk\\DLs\\'
        dir_content = os.listdir(dir_path)
        file = os.path.join(dir_path, random.choice(dir_content))
        # Replace file with any song you may have.
        y, sr = librosa.load(file, mono=False, duration=None)
        fig = Figure(figsize=(5, 2))

        super().__init__(fig)

        col = librosa.display.waveplot(y, sr=sr)

        self.axes = fig.gca()
        self.axes.add_collection(col)

        self.draw()
        self.show()



if __name__ == '__main__':

    app = QApplication(sys.argv)
    form = WaveForm()
    app.exec()
4

1 回答 1

0

这个问题通过设置 waveplot 采用一个名为 ax 的关键字参数来解决,在这里你给它坐标轴。您不需要存储 col 变量,也不需要在其上存储 add_collection。

librosa.display.waveplot(y, sr=sr, ax=self.axes)  
于 2018-06-05T19:58:53.397 回答