[英]ValueError: x and y must have same first dimension, but have shapes (99955,) and (15000,)
So I am trying to plot my.wav file using matplotlib.所以我正在尝试使用 matplotlib 来 plot my.wav 文件。 This is my list of code
这是我的代码列表
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)
But I kept facing with this kind of error that say my x and y don't have same first dimension.但我一直面临这种错误,说我的 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,)
What should I do?我应该怎么办?
Use np.arange insted of linspace got to know from here!使用 np.arange insted 的 linspace 从这里开始了解! by doing this it matches the both shape of x and y cordinates in plot
通过这样做,它匹配 plot 中 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.