【问题标题】:TypeError: float() argument must be a string or a number, not 'PolyCollection'TypeError:float() 参数必须是字符串或数字,而不是“PolyCollection”
【发布时间】:2020-07-25 17:33:17
【问题描述】:

我正在尝试使用librosa.display.waveplot(y,sr) 绘制音频文件

我的代码:

import librosa.display
ax1 = plt.subplot(gs[1])
y, sr = librosa.load("Audiofilepath")
ax1.plot(librosa.display.waveplot(y, sr))

我得到了绘制的结果。即使我收到以下错误消息

TypeError: float() argument must be a string or a number, not 'PolyCollection'

我在 StackOverflow 中使用上述错误消息进行了探索,结果可用于 numbers, period, nanType。但不适用于PolyCollection。

如果有librosa绘图的专家,请指导我避免此错误。

【问题讨论】:

    标签: python-3.x matplotlib audio librosa


    【解决方案1】:

    我认为您遵循了错误的文档。 librosa.display.waveplot()的文档可以在https://librosa.org/doc/latest/generated/librosa.display.waveplot.html找到。

    librosa.display.waveplot() 本身不绘制任何内容,您必须调用 plt.show() 才能将其可视化。

    import librosa
    import librosa.display
    import matplotlib.pyplot as plt
    
    plt.figure()
    
    y, sr = librosa.load("1.mp3")
    librosa.display.waveplot(y, sr)
    
    plt.title('Example of librosa.display.waveplot')
    
    plt.tight_layout()
    plt.show()
    

    librosa.display.waveplot() 的返回类型是matplotlib.collections.PolyCollection。 matplotlib.axes.Axes.plot 的参数类型可以是类数组或标量。

    但是,您将PolyCollection 传递给matplotlib.axes.Axes.plot。这就是为什么 TypeError: float() argument must be a string or a number, not 'PolyCollection' 被 ax1.plot(librosa.display.waveplot(y, sr)) 提出的原因。

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 2017-08-29
      • 1970-01-01
      • 2019-07-07
      相关资源
      最近更新 更多