服务器上使用matplotlib.pyplot绘图

Posted congyuuestcer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了服务器上使用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

 

以上是关于服务器上使用matplotlib.pyplot绘图的主要内容,如果未能解决你的问题,请参考以下文章

Python 绘图包 Matplotlib Pyplot 教程

使用 matplotlib.pyplot、imshow() 和 savefig() 以全分辨率绘图?

如何为 matplotlib.pyplot.matshow 绘图添加标签和标题

Python 绘图:如何让 matplotlib.pyplot 停止强制标记我的样式?

Matplotlib.pyplot.plot 绘图

dpplot命令安装