简体   繁体   中英

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. 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.

---------------------------------------------------------------------------
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! by doing this it matches the both shape of x and y cordinates in plot

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM