python - 无法在 python 3 中导入 matplotlib.pyplot

标签 python matplotlib importerror cairo

我按照说明从此处安装适用于 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]

最佳答案

问题原因 在 macOS 图像渲染 matplotlib 后端中(默认使用 Cocoa API 渲染的后端)。有 Qt4Agg 和 GTKAgg,并且作为后端不是默认的。设置与其他 Windows 或 Linux 操作系统不同的 macOS 后端。

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

~ / .matplotlib / Matplotlibrc
backend: TkAgg

关于python - 无法在 python 3 中导入 matplotlib.pyplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17393928/

相关文章:

python - 对彩色图像 OpenCV 使用 matchTemplate

python - 什么控制 Tkinter 中的自动窗口大小调整?

Matplotlib 较暗的 HSV 颜色图

python - matplotlib:箱线图对象中的传单设置不正确

python - pyinstaller exe 未运行 - 没有名为 Pyside 的模块

Python3导入错误

python - 动态创建变量是个好主意吗?

python - 如何连贯地组织 PyGTK 桌面应用程序的模块?

python - 从函数返回子图

python - 从转换器导入 BertTokenizer 时出现导入错误