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

 

 

 

 

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

https://blog.csdn.net/hurry0808/article/details/78013691

 

posted on 2018-12-31 14:02  Angry_Panda  阅读(2885)  评论(0编辑  收藏  举报

导航