天天看点

python 安装matplotlibUbuntu安装Matplotlibubuntu下安装matplotlib的复杂度远远比windows下复杂的多,相对双击就能解决问题的,现在你需要时不时的解决编译带来的各种问题。

1

<code>sudo apt-get install python-dev</code>

先安装numpy:

2

<code>python setup.py build</code>

<code>sudo python setup.py install --prefix=/usr/local</code>

之后开始安装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

解决方法:sudo apt-get install  libfreetype6-dev

3. src/backend_agg.cpp:3:17: fatal error: png.h: no such file or

解决方法:sudo apt-get install libpng-dev

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

<code>sudo python setup.py install</code>

检测下之前安装的情况:

&gt;&gt;&gt; import numpy

&gt;&gt;&gt; print

numpy.version.version

1.6.1

&gt;&gt;&gt; import matplotlib

&gt;&gt;&gt;

print matplotlib.__version__

0.99.3

到此,基本搞定。接下里,运行个sample试试看。

3

4

5

6

7

8

9

10

11

<code>from</code>

<code>pylab</code><code>import</code>

<code>*</code>

<code>t</code><code>=</code>

<code>arange(</code><code>0.0</code><code>,</code><code>2.0</code><code>,</code><code>0.01</code><code>)</code>

<code>s</code><code>=</code>

<code>sin(</code><code>2</code><code>*</code><code>pi</code><code>*</code><code>t)</code>

<code>plot(t, s, linewidth</code><code>=</code><code>1.0</code><code>)</code>

<code>xlabel(</code><code>‘time (s)‘</code><code>)</code>

<code>ylabel(</code><code>‘voltage (mv)‘</code><code>)</code>

<code>title(</code><code>‘about as simple as it gets, folks‘</code><code>)</code>

<code>grid(</code><code>true</code><code>)</code>

<code>show()</code>

终端执行: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()))

当然你如果只想要看结果,那么可以直接把它保存成图片,用<code>savefig</code>(‘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:

期间,我按装过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

这样就可以了。测试:

python 安装matplotlibUbuntu安装Matplotlibubuntu下安装matplotlib的复杂度远远比windows下复杂的多,相对双击就能解决问题的,现在你需要时不时的解决编译带来的各种问题。