0
点赞
收藏
分享

微信扫一扫

Linux服务器没有GUI的情况下使用matplotlib绘图

最近看到关于 python3 中用matplotlib 不进行交互画图,而是直接将图保存到硬盘,主要的一个设置就是  matplotlib.use('agg')


注明:

其实不设置  matplotlib.use('agg') 也可以把图片保存到硬盘,但是设置了这条语句再调用 matplotlib.pyplot.show 等进行交互式图片查看就会报错。

可以这么理解,设置了  matplotlib.use('agg') 后便强制你不能交互式查看显示图片,而只能保存到磁盘再查看。

下面对其进行一些介绍:

import matplotlib 

matplotlib.use('Agg')

#而且matplotlib.use('Agg')必须添加在 import matplotlib.pyplot 之前,否则无效
import matplotlib.pyplot as plt


#最后在后面加上
plt.savefig('/tmp/figure_2_1.png')
#就可以把绘制的图存为png,down到本地查看就行了

下面给出  matplotlib.use  的官方API介绍:

api 地址:  ​​ https://matplotlib.org/api/matplotlib_configuration_api.html?highlight=use#matplotlib.use​​

Linux服务器没有GUI的情况下使用matplotlib绘图_显示图片



在下面博客中也发现这样的介绍:


Linux服务器没有GUI的情况下使用matplotlib绘图_matplotlib(Python)_02


举报

相关推荐

0 条评论