您的位置:首页 > 运维架构 > Linux

Python学习-centos7.0下matplotlib安装及问题解决

2015-07-19 16:22 901 查看
Windows下matplotlib的安装,前面已经介绍了Python学习-windows安装Python以及matplotlib.pyplot包,由于现在在服务器上跑程序,需要在centos 7.0上安装,那么就做个笔记。

首先matplotlib是需要numpy先行包支持的,这里,我已经安装了numpy,下面安装matplotlib。

matplot需要一些其他软件支持

这时需要安装freetype 和 png 这两个库

yum install freetype freetype-devel python-freetype
yum install libpng libpng-devel python-pypng


然后再继续安装,前提是安装了pip:Python学习-安装pip和scikit-learn

然后执行下列命令即可

pip install matplotlib


最后使用pip安装报错:

/usr/bin/ld: /usr/local/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libpython2.7.a: could not read symbols: Bad value


参考资料:



/usr/local/bin
目录下的
libpython2.7.a
暂时保存为备份文件

cd /usr/local/bin
mv libpython2.7.a libpython2.7.a.tmp


然后执行

pip install matplotlib


就可以成功安装了了。

然后需要还原
libpython2.7.a


mv libpython2.7.a.tmp libpython2.7.a


执行Python的使用,将pyplot导入的时候,需要做下处理:

import matplotlib
matplotlib.use('Agg')
from matplotlib.pyplot import *


参考

DISPLAY error matplotlib
When error occur about this "$DISPLAY not set" when you run Python code that use matplotlib this happened because your matplotlib backend is set to FltkAgg, GTK, GTKAgg, GTKCairo, TkAgg , Wx or WxAgg they required a GUI that why error occur. 

To solve this you must specific other backend that not required GUI (Agg, Cairo, PS, PDF or SVG ) when use matplotlib like this

In code
import matplotlib
matplotlib.use('Agg')
In command line use -d option
Python subplot_demo.py -dAgg 

Remember when call savefig('filename') don't give it extension this will handle by backend that you specific e.g Agg will create file filename.png

source from --> http://matplotlib .sourceforge.net/backends.html


资料来源http://chewpichai.blogspot.com/2008/01/display-error-matplotlib.html (需***–推介红杏)。

说明,上面命令安装出错,也可以使用
yum
安装,安装命令

yum install python-matplotlib


测试:

[root@master bin]# python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>>


但是使用pyplot时还是报错了。。。使用的方法还是在使用代码时引用pyplot时修改下:

import matplotlib
matplotlib.use('Agg')
from matplotlib.pyplot import *


最后,安装pyplot库花了比较长的时间,一直出问题,有点恶心了。不过最后还是解决了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: