【问题标题】:using matplotlib with ipython console (spyder) fails使用带有 ipython 控制台(spyder)的 matplotlib 失败
【发布时间】:2018-11-08 12:36:44
【问题描述】:

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

【问题讨论】:

    标签: python numpy matplotlib anaconda spyder


    【解决方案1】:

    您似乎使用了 matplotlib 的 pyplot 模块(这就是 plt 通常代表的意思)。它应该像这样导入:

    import matplotlib.pyplot as plt
    

    如果我更改该行,您的代码对我来说非常有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2020-11-19
      • 2018-02-02
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多