使用matplotlib和ipython控制台(spyder)失败

时间:2018-05-29 19:14:51

标签: python numpy matplotlib anaconda spyder

我正在尝试在Spyder中运行以下代码(使用IPython控制台):

import numpy as np
import matplotlib as plt

labels = ['python\nloop', 'numpy\nbroadc.', 'sklearn', 'fortran/\nf2py', 'scipy', 'cython', 'numba']
timings = [13.4, 0.111, 0.0356, 0.0167, 0.0129, 0.00987, 0.00912]
x = np.arange(len(labels))

ax = plt.axes(xticks=x, yscale='log')
ax.bar(x - 0.3, timings, width=0.6, alpha=0.4, bottom=1E-6)
ax.grid()
ax.set_xlim(-0.5, len(labels) - 0.5)
ax.set_ylim(1E-3, 1E2)
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda i, loc: labels[int(i)]))
ax.set_ylabel('time (s)')
ax.set_title("Pairwise Distance Timings")

但是,我收到以下错误:

Traceback (most recent call last):

  File "<ipython-input-1-46193480d26c>", line 1, in <module>
    runfile('C:/Users/owner/.spyder-py3/temp.py', wdir='C:/Users/owner/.spyder-py3')

  File "C:\Users\owner\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\owner\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/owner/.spyder-py3/temp.py", line 9, in <module>
    ax = plt.axes(xticks=x, yscale='log')

TypeError: 'module' object is not callable

我错过了什么吗?据我所知,这段代码在IDLE中运行良好,但我无法用PIP安装NumPy,所以我采用了简单的方法并安装了Anaconda。

1 个答案:

答案 0 :(得分:1)

您似乎使用了matplotlib的pyplot模块(plt通常代表的是什么)。它应该导入如下:

import matplotlib.pyplot as plt

如果我改变那一行,你的代码对我来说非常有用:)