Matplotlib yscale设置值错误:无法将浮点NaN转换为整数

2024-05-29 04:39:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将y轴的比例转换为log by plt.yscale('log')

def gen_spectrograms(wav):

    fs, x = scipy.io.wavfile.read(wav) # read audio file as np array
    spec, freqs, _, _= plt.specgram(x, Fs=fs, NFFT=2048, noverlap=1900, mode='default')
    plt.close('all')
    gc.collect()

    spec_ = spec[:600, :]
    spec_ = spec_[::-1]
    spec_ = cv2.resize(spec_, dsize=(128, 256), interpolation=cv2.INTER_CUBIC)

    return spec_


spec = gen_spectrograms(p)
plt.yscale('log')
plt.imshow(spec)

整个错误报告:

ValueError                                Traceback (most recent call last)
/Users/Terry/anaconda/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
    335                 pass
    336             else:
--> 337                 return printer(obj)
    338             # Finally look for special method names
    339             method = _safe_get_formatter_method(obj, self.print_method)

/Users/Terry/anaconda/lib/python3.5/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
    205 
    206     if 'png' in formats:
--> 207         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    208     if 'retina' in formats or 'png2x' in formats:
    209         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/Users/Terry/anaconda/lib/python3.5/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    115 
    116     bytes_io = BytesIO()
--> 117     fig.canvas.print_figure(bytes_io, **kw)
    118     data = bytes_io.getvalue()
    119     if fmt == 'svg':

/Users/Terry/anaconda/lib/python3.5/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2230                 orientation=orientation,
   2231                 bbox_inches_restore=_bbox_inches_restore,
-> 2232                 **kwargs)
   2233         finally:
   2234             if bbox_inches and restore_bbox:

/Users/Terry/anaconda/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
    525 
    526     def print_png(self, filename_or_obj, *args, **kwargs):
--> 527         FigureCanvasAgg.draw(self)
    528         renderer = self.get_renderer()
    529         original_dpi = renderer.dpi

/Users/Terry/anaconda/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    467         if __debug__: verbose.report('FigureCanvasAgg.draw', 'debug-annoying')
    468 
--> 469         self.renderer = self.get_renderer(cleared=True)
    470         # acquire a lock on the shared font cache
    471         RendererAgg.lock.acquire()

/Users/Terry/anaconda/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in get_renderer(self, cleared)
    484 
    485         if need_new_renderer:
--> 486             self.renderer = RendererAgg(w, h, self.figure.dpi)
    487             self._lastKey = key
    488         elif cleared:

/Users/Terry/anaconda/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py in __init__(self, width, height, dpi)
     91         self.height = height
     92         if __debug__: verbose.report('RendererAgg.__init__ width=%s, height=%s'%(width, height), 'debug-annoying')
---> 93         self._renderer = _RendererAgg(int(width), int(height), dpi, debug=False)
     94         self._filter_renderers = []
     95 

ValueError: cannot convert float NaN to integer

我怀疑发生错误是因为某些像素值太小?请给我一些建议。非常感谢。你知道吗


4月23日11:20更新

我已将代码简化为:

wav = 'audio.wav'
fs, x = scipy.io.wavfile.read(wav) # read audio file as np array
spec, _, _, _ = plt.specgram(x, Fs=fs, NFFT=2048, noverlap=1900, mode='default')
plt.yscale('log')
plt.imshow(spec)

错误报告是:

/home/gty/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py:3477: UserWarning: Attempted to set non-positive ylimits for log-scale axis; invalid limits will be ignored.
  'Attempted to set non-positive ylimits for log-scale axis; '
<matplotlib.image.AxesImage at 0x7f6dd803ada0>
/home/gty/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py:1579: UserWarning: aspect is not supported for Axes with xscale=linear, yscale=log
  'yscale=%s' % (xscale, yscale))
/home/gty/anaconda3/lib/python3.7/site-packages/matplotlib/transforms.py:2745: RuntimeWarning: divide by zero encountered in double_scalars
  y_scale = 1.0 / inh
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
/home/gty/anaconda3/lib/python3.7/site-packages/matplotlib/transforms.py:416: RuntimeWarning: invalid value encountered in double_scalars
  return points[1, 0] - points[0, 0]

Tags: andinpyselfmatplotliblibpackagessite

热门问题