Skip to main content
 首页 » 编程设计

python之运行示例时出错

2025年05月04日8rubylouvre

我正在尝试使用 numpy.fft.fft。

当我运行本页底部的示例 (numpy.fft.fft) 时,出现错误并且图形很奇怪。

如何做到正确?我猜错误与 backend_macosx.py 有关?

以下是全部代码:

>>> import matplotlib.pyplot as plt 
>>> import numpy as np 
>>> t = np.arange(256) 
>>> sp = np.fft.fft(np.sin(t)) 
>>> freq = np.fft.fftfreq(t.shape[-1]) 
>>> plt.plot(freq, sp.real, freq, sp.imag) 
[<matplotlib.lines.Line2D object at 0x1046c8c50>, <matplotlib.lines.Line2D object at 0x1046c8ed0>] 
>>> plt.show() 
Traceback (most recent call last): 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1034, in draw 
    func(*args) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw 
    a.draw(renderer) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/axis.py", line 1093, in draw 
    renderer) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/axis.py", line 1042, in _get_tick_bboxes 
    extent = tick.label1.get_window_extent(renderer) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 754, in get_window_extent 
    bbox, info, descent = self._get_layout(self._renderer) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 320, in _get_layout 
    ismath=False) 
  File "/Users/xujc/.Software/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 158, in get_text_width_height_descent 
    width, height, descent = self.gc.get_text_width_height_descent(unicode(s), family, size, weight, style) 
TypeError: must be string, not int 

附言:

$python 
Python 2.7.7 |Anaconda 2.0.1 (x86_64)| (default, Jun  2 2014, 12:48:16) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
Anaconda is brought to you by Continuum Analytics. 
Please check out: http://continuum.io/thanks and https://binstar.org 

matplotlib 版本为 1.3.1。

请您参考如下方法:

对我来说工作正常,如果你点击小“>>>”你会得到这样的东西:

import matplotlib.pyplot as plt 
import numpy as np 
t = np.arange(256) 
sp = np.fft.fft(np.sin(t)) 
freq = np.fft.fftfreq(t.shape[-1]) 
plt.plot(freq, sp.real, freq, sp.imag) 
 
plt.show() 

python 版本

Python 2.7.6 (default, Jul  9 2014, 20:49:24)  
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.38)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 

Matplotlib..

matplotlib (1.3.1) 

所以尝试返回使用 python 的 mac 发行版,因为改变它会产生更多它可以解决的问题。

我使用 pip 安装所有库。 :)

结果::