>>> import matplotlib.pyplot as plt
当运行上面的简单import语句时,出现了下面的错误:
Traceback (most recent calllast):
File "
",line 1, in File"/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py",line 115, in
_backend_mod,new_figure_manager, draw_if_interactive, _show = pylab_setup()
File"/usr/local/lib/python2.7/dist-packages/matplotlib/backends/__init__.py",line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File"/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4agg.py",line 18, in
from .backend_qt5agg importFigureCanvasQTAggBase as _FigureCanvasQTAggBase
File"/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt5agg.py",line 16, in
from .backend_qt5 importQtCore
File"/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt5.py",line 26, in
importmatplotlib.backends.qt_editor.figureoptions as figureoptions
File"/usr/local/lib/python2.7/dist-packages/matplotlib/backends/qt_editor/figureoptions.py",line 20, in
importmatplotlib.backends.qt_editor.formlayout as formlayout
File"/usr/local/lib/python2.7/dist-packages/matplotlib/backends/qt_editor/formlayout.py",line 56, in
frommatplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore
File"/usr/local/lib/python2.7/dist-packages/matplotlib/backends/qt_compat.py",line 179, in
"Matplotlib qt-basedbackends require an external PyQt4, PyQt5,\n"
ImportError:Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to beinstalled, but it was not found.
只要import matplotlib.pyplot 就会出现这种错误,以前没有出现过,按错误提示去install PySide时又出现别的问题,查
了查,像是因为装QT5引起的,Ubuntu16.04系统自带的是QT4,我装完QT5后发现/usr/lib/x86_64-linux-gnu/qt4/bin
下面没有可执行文件了,无奈只能把QT4再安装一下了
(1) 安装QT4
username@machine:~$ sudo apt-get install build-essential
username@machine:~$ sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demosqt4-designer
(2) 安装PySide
username@machine:~$ sudo pip install PySide -i https://pypi.tuna.tsinghua.edu.cn/simple #-i后面是一个镜像
使用下面的方法也可以解决上面的问题,只是多了一两行代码
>>> import matplotlib
>>> matplotlib.use(“Agg”) # 或着matplotlib.use(“GTK”)或者matplotlib.use(“Qt5Agg”)
>>> importmatplotlib.pyplot as plt
matplotlib.use()用来指定matplotlib用哪个backend
matplotlib.get_backend()可以查看matplotlib当前用的backend是哪个
/usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf这个链接文件可以查看当前使用哪个版本的QT