Ubuntu安装Matplotlib

回想整个安装过程,ubuntu下安装matplotlib的复杂度远远比windows下复杂的多,相对双击就能解决问题的,现在你需要时不时的解决编译带来的各种问题。

系统默认是安装了python 2.7的,但是安装其他package的时候还是提示你安装python-dev:(SystemError: Cannot compile ‘Python.h’. Perhaps you need to install python-dev|python-devel.)

sudo apt-get install python-dev

安装之后,开始准备Matplotlib所需Numpy,Matplotlib。两个package都可以从对应的官网下载。Google自行搜索……

先安装numpy:

python setup.py build
sudo python setup.py install --prefix=/usr/local

之后开始安装matplotlib,这下编译matplotlib就没有之前那样一帆风顺了。

1. gcc: error trying to exec ‘cc1plus’: execvp: No such file or directory
解决方法:sudo apt-get install build-essential

2. src/ft2font.h:13:22: fatal error: ft2build.h: No such file or directory
解决方法:sudo apt-get install  libfreetype6-dev

3. src/backend_agg.cpp:3:17: fatal error: png.h: No such file or directory
解决方法:sudo apt-get install libpng-dev

解决以上问题之后,发现可以正确编译matplotlib了:

python setup.py build
sudo python setup.py install

检测下之前安装的情况:

>>> import numpy
>>> print numpy.version.version
1.6.1
>>> import matplotlib
>>> print matplotlib.__version__
0.99.3
到此,基本搞定。接下里,运行个Sample试试看。

from pylab import *

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s, linewidth=1.0)

xlabel('time (s)')
ylabel('voltage (mV)')
title('About as simple as it gets, folks')
grid(True)
show()

终端执行:python hello.py 没有报错,也没有弹出图框。怎么回事? 我尝试把代码中每一条都手动在终端python模式下输入,结果输入show()的时候,错误提示:
Your currently selected backend, ‘agg’ does not support show().
Please select a GUI backend in your matplotlibrc file (‘/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc’)
or with matplotlib.use()
(backend, matplotlib.matplotlib_fname()))

当然你如果只想要看结果,那么可以直接把它保存成图片,用savefig(‘figure.png’)来替代前面的show()函数。但是如果要交互式的话,还需解决前面的问题。

这个问题,我找了很久,发现”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')

期间,我按装过Cairo,可是还是出现错误,后来发现一个比较简单的方法,用wxpython:
sudo aptitude install python-wxtools
然后在代码中使用的是matplotlib.use(‘WXAgg’)

你也可以修改/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data目录下的matplotlibrc这个文件内容中的:

# ‘module://my_backend’
backend      : WXAgg

这样就可以了。测试:

from pylab import *

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s, linewidth=1.0)

xlabel('time (s)')
ylabel('voltage (mV)')
title('About as simple as it gets, folks')
grid(True)
show()

matplotlib

你可能还感兴趣的相关文章:

    • 散步的小龟
    • 2012年05月8日

    散步的小龟 :
    评论等待审核中
    正在头疼呢,多谢啦!

    PS: 按照lz的提示安装好了。安装过程中在某官网上看到numpy可以这样安装
    sudo python setup.py build
    sudo python setup.py install –user

    心想安装matplotlib是不是也可以加后缀 –user,事实证明这样安装好后在python下import matplotlib时会出错,还是用lz这种默认安装方式才行啊!!

    但愿没人和我犯一样的错误~

    :P

      • Leyond
      • 2012年05月8日

      多多折腾就好了

    • 散步的小龟
    • 2012年05月18日

    嗯~ :)

    Leyond :
    多多折腾就好了

  1. 没有通告