运行示例时出错

时间:2014-07-28 05:16:58

标签: python numpy matplotlib

我正在尝试使用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

figure

PS:

$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

1 个答案:

答案 0 :(得分:1)

如果您点击小&#34;&gt;&gt;&gt;&#34;你会得到这样的东西:

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安装所有库。 :)

结果:: enter image description here