Installing matplotlib, NumPy, and SciPy
This chapter describes several ways of installing matplotlib and required dependencies under Linux.
Getting ready
We assume that you already have Linux (preferably Debian/Ubuntu or RedHat/SciLinux) installed and Python installed on it. Usually, Python is already installed on the mentioned Linux distributions and, if not, it is easily installable through standard means. We assume that Python 2.7+ Version is installed on your workstation.
Note
Almost all code should work with Python 3.3+ Versions, but because most operating systems still deliver Python 2.7 (some even Python 2.6) we decided to write the Python 2.7 Version code. The differences are small, mainly in version of packages and some code (xrange should be substituted with range in Python 3.3+).
We also assume that you know how to use your OS package manager in order to install software packages and know how to use a terminal.
Build requirements must be satisfied before matplotlib can be built.
matplotlib requires NumPy, libpng, and freetype as build dependencies. In order to be able to build matplotlib from source, we must have installed NumPy. Here's how to do it:
Install NumPy (at least 1.4+, or 1.5+ if you want to use it with Python 3) from http://www.numpy.org/.
Note
NumPy will provide us with data structures and mathematical functions for using it with large datasets. Python's default data structures such as tuples, lists, or dictionaries are great for insertions, deletions, and concatenation. NumPy's data structures support "vectorized" operations and are very efficient for use and for executions. They are implemented with Big Data in mind and rely on C implementations that allow efficient execution time.
SciPy, building on top of NumPy, is the de facto standard's scientific and numeric toolkit for Python comprising great selection of special functions and algorithms, most of them actually implemented in C and Fortran, coming from the well-known Netlib repository (see http://www.netlib.org).
Perform the following steps for installing NumPy:
Install Python-NumPy package:
$ sudo apt-get install python-numpy
Check the installed version:
$ python -c 'import numpy; print numpy.__version__'
Install the required libraries:
libpng 1.2: PNG files support (requires zlib)
freetype 1.4+: True type font support
$ sudo apt-get install build-dep python-matplotlib
If you are using RedHat or variation of this distribution (Fedora, SciLinux, or CentOS) you can use yum to perform same installation:
$ su -c 'yum-builddep python-matplotlib'
How to do it...
There are many ways one can install matplotlib and its dependencies: from source, from precompiled binaries, from OS package manager, and with prepackaged python distributions with built-in matplotlib.
Most probably the easiest way is to use your distribution's package manager. For Ubuntu that should be:
# in your terminal, type: $ sudo apt-get install python-numpy python-matplotlib python-scipy
If you want to be on the bleeding edge, the best option is to install from source. This path comprises a few steps: Get the source, build requirements, and configure, compile, and install.
Download the latest source from code host www.github.com by following these steps:
$ cd ~/Downloads/ $ wget https://github.com/downloads/matplotlib/matplotlib/matplotlib-1.2.0.tar.gz $ tar xzf matplotlib-1.2.0.tar.gz $ cd matplotlib-1.2.0 $ python setup.py build $ sudo python setup.py install
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
How it works...
We use standard Python Distribution Utilities, known as Distutils, to install matplotlib from source code. This procedure requires us to previously install dependencies, as we already explained in the Getting ready section of this recipe. The dependencies are installed using the standard Linux packaging tools.
There's more...
There are more optional packages that you might want to install depending on what your data visualization projects are about.
No matter what project you are working on, we recommend installing IPython—an Interactive Python shell that supports PyLab mode where you already have matplotlib and related packages, such as NumPy and SciPy, imported and ready to play with! Please refer to IPython's official site on how to install it and use it—it is, though, very straightforward.