[英]Cannot import matplotlib.pyplot in python 3
我從這里按照說明為python 3 安裝matplotlib : 如何使用Python3.2安裝matplotlib
但是我在導入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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.