python - 运行示例时出错

标签 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

附言:

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

结果:: enter image description here

关于python - 运行示例时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988800/

相关文章:

python - 在 Python 中使用 matplotlib 转换数组列表以进行绘图

python - 将 pandas 数据帧的字符串列转换为 0 1 向量

python - 如何标准化 scipy 中指数分布的直方图?

python - 如何在instance.save()之后删除input.txt文件的所有内容?

python - IB API 无法在 Mac 上正确安装

python - 导入 scipy.sparse 失败

python - 绘制阈值(precision_recall 曲线) matplotlib/sklearn.metrics

python - Matplotlib - 使用 for 循环绘制 3D 图形

python - 如何选择哪个多索引轴将 groupby 对象中的数据拆分到不同的子图中?

python - 如何获取多类别的所有混淆矩阵术语(TPR、FPR、TNR、FNR)?