【问题标题】:Cannot import matplotlib.pyplot in python 3无法在 python 3 中导入 matplotlib.pyplot
【发布时间】:2013-06-30 19:12:27
【问题描述】:

我按照说明从这里为 python 3 安装 matplotlib:How to install matplotlib with Python3.2

但我在导入 pyplot 时遇到 ImportError,我无法解决:

>>> import matplotlib
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.4.x-py3.2-linux-i686.egg/matplotlib/pyplot.py", line 98, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
 File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.4.x-py3.2-linux-i686.egg/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
 File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.4.x-py3.2-linux-i686.egg/matplotlib/backends/backend_gtk3agg.py", line 1, in <module>
   import cairo
ImportError: No module named cairo 

我在导入 pyplot 之前尝试过 matplotlib.use('Agg')matplotlib.use('GTK'),但它们没有任何区别。

任何帮助将不胜感激,谢谢。

编辑:
这表明我拥有所有必需的依赖项。

jack@hostname:~/installs/matplotlib$ sudo python3 setup.py --keywords
============================================================================
Edit setup.cfg to change the build options

BUILDING MATPLOTLIB
        matplotlib: yes [1.4.x]
            python: yes [3.2.3 (default, Apr 10 2013, 05:29:11)  [GCC
                    4.6.3]]
          platform: yes [linux2]

REQUIRED DEPENDENCIES AND EXTENSIONS
             numpy: yes [version 1.6.1]
          dateutil: yes [using dateutil version 2.1]
           tornado: yes [using tornado version 3.1]
         pyparsing: yes [using pyparsing version 2.0.0]
             pycxx: yes [Official versions of PyCXX are not compatible
                    with Python 3.x.  Using local copy]
            libagg: yes [pkg-config information for 'libagg' could not
                    be found. Using local copy.]
          freetype: yes [version 14.0.8]
               png: yes [version 1.2.46]

OPTIONAL SUBPACKAGES
       sample_data: yes [installing]
          toolkits: yes [installing]
             tests: yes [using nose version 1.3.0]

OPTIONAL BACKEND EXTENSIONS
            macosx: no  [Mac OS-X only]
            qt4agg: no  [PyQt4 not found]
           gtk3agg: yes [gtk3agg backend does not work on Python 3]
         gtk3cairo: no  [Requires cairo to be installed.]
            gtkagg: no  [Requires pygtk]
             tkagg: yes [version version not identified]
             wxagg: no  [requires wxPython]
               gtk: no  [Requires pygtk]
               agg: yes [installing]
             cairo: no  [cairo not found]
         windowing: no  [Microsoft Windows only]

OPTIONAL LATEX DEPENDENCIES
            dvipng: yes [version 1.14]
       ghostscript: yes [version 9.05]
             latex: yes [version 3.1415926]
           pdftops: yes [version 0.18.4]

【问题讨论】:

  • 你是如何安装 matplotlib 的?使用 apt-get 或 setup.py 等包管理器?
  • 你可能还没有安装pycairo
  • “...它们没有任何区别”,因为您仍然会得到 an ImportErrorthis special ImportError?
  • @KenHill 我从 Github 克隆,然后执行 'sudo python3 setup.py build',然后执行 'sudo python3 setup.py install'
  • @mata 我安装了python-cairo,然后重新构建并重新安装是一样的吗?

标签: python matplotlib importerror cairo


【解决方案1】:

问题原因 在 mac os 中 matplotlib 的图像渲染后端(what-is-a-backend 默认使用 Cocoa 的 API 渲染)。有 Qt4Agg 和 GTKAgg 并且作为后端不是默认的。设置与其他windows或linux操作系统不同的macosx后端。

我假设您已经安装了 pip matplotlib,我通过以下方式解决了这个问题,您的根目录中有一个名为 ~ / 的目录。 matplotlib。在那里创建一个名为 matplotlibrc 的文件和以下代码

~ / .matplotlib / Matplotlibrc
backend: TkAgg

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2017-05-30
    • 2013-04-07
    相关资源
    最近更新 更多