服务器上使用matplotlib.pyplot绘图
在linux服务器端执行python脚本,有时候需要画图,但是linux没有GUI界面,因此需要在导入matplotlib.pyplot库之前先执行
import matplotlib as mpl mpl.use('Agg')
再执行
import matplotlib.pyplot as plt
需要保存图片到指定的目录
plt.savefig("/home/yourname/test.jpg")
直接执行 import matplotlib.pyplot as plt 会报错:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 116, in <module> _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 60, in pylab_setup [backend_name], 0) File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 16, in <module> from .backend_qt5 import ( File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 18, in <module> import matplotlib.backends.qt_editor.figureoptions as figureoptions File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/qt_editor/figureoptions.py", line 20, in <module> import matplotlib.backends.qt_editor.formlayout as formlayout File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/qt_editor/formlayout.py", line 56, in <module> from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/qt_compat.py", line 137, in <module> from PyQt5 import QtCore, QtGui, QtWidgets ImportError: libGL.so.1: cannot open shared object file: No such file or directory