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 os不同的macosx后端。

我以假定您已安装pip matplotlib的方式解决了此问题,您的根目录中有一个名为〜/的目录。 matplotlib。在其中创建一个名为matplotlibrc的文件,然后执行以下代码

~ / .matplotlib / Matplotlibrc
backend: TkAgg

相关文章:

python - 使用scipy.integrate将布朗运动纳入粒子轨迹积分

python - cartopy:较大的圆圈距离线具有更高的分辨率

python - 使用Matplotlib绘制多级标题数据框

python - Python,ImportError:无法导入名称AbstractLazySequence

c# - AES加密。从Python(pyCrypto)到.NET

python - Python,使用dict进行有效的并行操作

python - python twitter api每秒返回的推文数量不一致

python - 将Unicode文本与seaborn一起使用

python - 无法在OSX El Capitan(opencv3.1.0)上的python2.7中导入cv2

go - 导入本地源文件以与Travis CI兼容