【问题标题】:ValueError: x and y must have same first dimension, but have shapes (99955,) and (15000,)ValueError: x 和 y 必须具有相同的第一个维度,但具有形状 (99955,) 和 (15000,)
【发布时间】:2023-03-24 20:20:01
【问题描述】:

所以我正在尝试使用 matplotlib 绘制我的 .wav 文件。这是我的代码清单

from scipy.io import wavfile
import numpy as np
import matplotlib.pyplot as plt

train_audio_path = 'input/train2/audio/'
filename = 'bu/uji-bu-051.wav'
sample_rate, samples = wavfile.read(train_audio_path + filename)

fig = plt.figure(figsize=(14, 8))
ax1 = fig.add_subplot(211)
ax1.set_title('Raw wave of ' + filename)
ax1.set_ylabel('Amplitude')
ax1.plot(np.linspace(0, sample_rate/len(samples), sample_rate), samples)

但我一直面临这种错误,说我的 x 和 y 没有相同的第一维。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-62-94bfcd54ac3d> in <module>
      5 ax1.set_title('Raw wave of ' + filename)
      6 ax1.set_ylabel('Amplitude')
----> 7 ax1.plot(np.linspace(0, sample_rate/len(samples), sample_rate), samples)
      8 
      9 # ax2 = fig.add_subplot(212)

~\.conda\envs\speechRecog\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1741         """
   1742         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1744         for line in lines:
   1745             self.add_line(line)

~\.conda\envs\speechRecog\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs)
    271                 this += args[0],
    272                 args = args[1:]
--> 273             yield from self._plot_args(this, kwargs)
    274 
    275     def get_next_color(self):

~\.conda\envs\speechRecog\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    397 
    398         if x.shape[0] != y.shape[0]:
--> 399             raise ValueError(f"x and y must have same first dimension, but "
    400                              f"have shapes {x.shape} and {y.shape}")
    401         if x.ndim > 2 or y.ndim > 2:

ValueError: x and y must have same first dimension, but have shapes (99955,) and (15000,)

我该怎么办?

【问题讨论】:

  • len(np.linspace(0, sample_rate/len(samples), sample_rate))len(samples) 返回什么?
  • 返回 66636 和 15000

标签: python python-3.x numpy matplotlib speech-recognition


【解决方案1】:

使用 np.arange insted of linspace got to know from here! 通过这样做它匹配绘图中 x 和 y 坐标的形状

from scipy.io import wavfile
import numpy as np
import matplotlib.pyplot as plt    

train_audio_path = 'input/train2/audio/'
filename = 'bu/uji-bu-051.wav'
sample_rate, samples = wavfile.read(train_audio_path + filename)
 
fig = plt.figure(figsize=(14, 8))
ax1 = fig.add_subplot(211)
ax1.set_title('Raw wave of ' + filename)
ax1.set_ylabel('Amplitude')
ax1.plot(np.arange(0, len(samples)/sample_rate, 1/sample_rate),samples)

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多