Python之matplotlib模塊安裝


numpy

1、下載安裝

源代碼

http://sourceforge.net/projects/numpy/files/NumPy/

安裝

python2.7 setup.py install

2、測試

導入numpy模塊,出現如下錯誤:

[root@typhoeus79 numpy-1.8.0]# python2.7
Python 2.7.3 (default, Nov 27 2012, 17:47:24) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "numpy/__init__.py", line 143, in <module>
    raise ImportError(msg)
ImportError: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python intepreter from there.
>>> 

原因是在源代碼安裝的地方進行測試,當前目錄有如下文件:

[root@typhoeus79 numpy-1.8.0]# ll
drwxr-xr-x 18 501 games 4096 Nov 13 17:57 numpy

換個目錄就ok了

>>> import numpy
>>> print numpy.__version__
1.9.0.dev-4d0076f

matplotlib

1、安裝准備

需要安裝six模塊

https://pypi.python.org/simple/six/

 

作用:
Six is a Python 2 and 3 compatibility library.  It provides utility functions
for smoothing over the differences between the Python versions with the goal of
writing Python code that is compatible on both Python versions.  See the
documentation for more information on what is provided.

 

需要安裝pyparsing

http://pyparsing.wikispaces.com/Download+and+Installation

說明:
The pyparsing module is an alternative approach to creating and executing 
simple grammars, vs. the traditional lex/yacc approach, or the use of 
regular expressions.  The pyparsing module provides a library of classes 
that client code uses to construct the grammar directly in Python code.

Here is a program to parse "Hello, World!" (or any greeting of the form 
"<salutation>, <addressee>!"):

    from pyparsing import Word, alphas
    greet = Word( alphas ) + "," + Word( alphas ) + "!"
    hello = "Hello, World!"
    print hello, "->", greet.parseString( hello )

The program outputs the following:

    Hello, World! -> ['Hello', ',', 'World', '!']

 

2、安裝matplotlib

http://sourceforge.net/projects/matplotlib/?source=dlp

python2.7 setup.py install
>>> import matplotlib
>>> print matplotlib.__version__
1.3.1

http://matplotlib.org/1.3.1/api/pyplot_summary.html 

3、例子

代碼:

"""
This example shows how to use a path patch to draw a bunch of
rectangles for an animated histogram
"""
import numpy as np

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
import matplotlib.animation as animation

fig, ax = plt.subplots()

# histogram our data with numpy
data = np.random.randn(1000)
n, bins = np.histogram(data, 100)

# get the corners of the rectangles for the histogram
left = np.array(bins[:-1])
right = np.array(bins[1:])
bottom = np.zeros(len(left))
top = bottom + n
nrects = len(left)

# here comes the tricky part -- we have to set up the vertex and path
# codes arrays using moveto, lineto and closepoly

# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
# CLOSEPOLY; the vert for the closepoly is ignored but we still need
# it to keep the codes aligned with the vertices
nverts = nrects*(1+3+1)
verts = np.zeros((nverts, 2))
codes = np.ones(nverts, int) * path.Path.LINETO
codes[0::5] = path.Path.MOVETO
codes[4::5] = path.Path.CLOSEPOLY
verts[0::5,0] = left
verts[0::5,1] = bottom
verts[1::5,0] = left
verts[1::5,1] = top
verts[2::5,0] = right
verts[2::5,1] = top
verts[3::5,0] = right
verts[3::5,1] = bottom

barpath = path.Path(verts, codes)
patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
ax.add_patch(patch)

ax.set_xlim(left[0], right[-1])
ax.set_ylim(bottom.min(), top.max())

def animate(i):
    # simulate new data coming in
    data = np.random.randn(1000)
    n, bins = np.histogram(data, 100)
    top = bottom + n
    verts[1::5,1] = top
    verts[2::5,1] = top

ani = animation.FuncAnimation(fig, animate, 100, repeat=False)

plt.savefig("./test.png") #測試環境屬於CentOS release 5.4非界面版
#plt.show() #故show不出來

結果:

3、ipython

ipython 是一個 python 的交互式 shell,比默認的python shell 好用得多,支持變量自動補全,自動縮進,支持 bash shell 命令,內置了許多很有用的功能和函數。

https://github.com/ipython/ipython

[root@typhoeus79 20131113]# ipython 
Python 2.4.3 (#1, Sep  3 2009, 15:37:37) 
Type "copyright", "credits" or "license" for more information.

IPython 0.8.4 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: %pycat test.
test.png  test.py   test.txt  

In [1]: %pycat test.txt
test ipython

 從python版本看還是2.4.3,使用python2.7 setup.py install安裝之后再次調用出現如下警告:

/usr/local/sinasrv2/lib/python2.7/site-packages/IPython/utils/path.py:424: UserWarning: Found old IPython config file u'/root/.ipython/ipy_user_conf.py' (modified by user)

將/root/.ipython移走即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM