Book Image

Matplotlib for Python Developers

Book Image

Matplotlib for Python Developers

Overview of this book

Providing appealing plots and graphs is an essential part of various fields such as scientific research, data analysis, and so on. Matplotlib, the Python 2D plotting library, is used to produce publication-quality figures in a variety of hardcopy formats and interactive environments across platforms. This book explains creating various plots, histograms, power spectra, bar charts, error charts, scatter-plots and much more using the powerful Matplotlib library to get impressive out-of-the-box results. This book gives you a comprehensive tour of the key features of the Matplotlib Python 2D plotting library, right from the simplest concepts to the most advanced topics. You will discover how easy it is to produce professional-quality plots when you have this book to hand. The book introduces the library in steps. First come the basics: introducing what the library is, its important prerequisites (and terminology), installing and configuring Matplotlib, and going through simple plots such as lines, grids, axes, and charts. Then we start with some introductory examples, and move ahead by discussing the various programming styles that Matplotlib allows, and several key features. Further, the book presents an important section on embedding applications. You will be introduced to three of the best known GUI libraries 'GTK+, Qt, and wxWidgets' and presented with the steps to implement to include Matplotlib in an application written using each of them. You will learn through an incremental approach: from a simple example that presents the peculiarities of the GUI library, to more complex ones, using GUI designer tools. Because the Web permeates all of our activities, a part of the book is dedicated to showing how Matplotlib can be used in a web environment, and another section focuses on using Matplotlib with common Python web frameworks, namely, Pylons and Django. Last, but not least, you will go through real-world examples, where you will see some real situations in which you can use Matplotlib.
Table of Contents (14 chapters)
Matplotlib for Python Developers
Credits
About the Author
About the Reviewers
Preface

Installing Matplotlib


There are several ways to install Matplotlib on our system:

  • Using packages from a Linux distribution

  • Using binary installers (for Windows and Mac OS X only)

  • Using packaged Python distributions that contain Matplotlib in the toolbox proposed

  • From the source code

We will look at each option in detail. We assume that Python, NumPy, and the optional build and runtime dependencies are already installed in the system (in order to install them, refer to their installation guides).

Installing Matplotlib on Linux

The advantage of using a Linux distribution is that several programs and libraries are already prepared by the distribution developers and made available (in a package format) to users. All we have to do is use the right tool and install the package.

In the following table, we will present some of the common Linux distributions package names for Matplotlib and the tools we can use to install the package:

Distribution

Package name

Installer tool

Debian or Ubuntu

(and all other Debian derivatives)

python-matplotlib

Synaptic (graphical)

apt-get or aptitude (command line)

Fedora

python-matplotlib

PackageKit (graphical)

yum or rpm (command line)

openSUSE

python-matplotlib

YaST (graphical)

zipper or rpm (command line)

Installing Matplotlib on Windows

Before we can install Matplotlib, we have to satisfy its main dependencies. So, we have to download:

  • Installers for Python, which are available in the DOWNLOAD section of http://www.python.org/

  • Installers for NumPy, which are available in the Download section of http://www.scipy.org/

Once we've got the above packages correctly installed, we can go to the main project page of Matplotlib on SourceForge at http://sourceforge.net/projects/matplotlib/. In the Files section, we can find the relative versions of the binary packages for the Python that we have just installed (2.4, 2.5, or 2.6).

Installing Matplotlib on Mac OS X

The procedure to install Matplotlib correctly on Mac OS X is similar to that of Windows.

First of all, we need to download:

  • Installers for Python, which are available in the DOWNLOAD section of http://www.python.org/ (the one already available in Mac OS X fives problem when using Matplotlib)

  • Installers for NumPy, which are available in the Download section of http://www.scipy.org/ or can be retrieved directly from http://pythonmac.org/

At this point, once they are correctly installed, we can download the binary installer from the download area of Matplotlib SourceForce page at http://sourceforge.net/projects/matplotlib/ or we can retrieve the version available at http://pythonmac.org/.

Installing Matplotlib using packaged Python distributions

There are some packaged distributions of Python that contain Matplotlib in them, along with many other tools, such as IPython, NumPy, SciPy, and so on. These distributions will set up all the necessary things we need so that we can use Matplotlib on our machine. Some of the distributions are as follows:

  • Enthought Python Distribution (EPD): This package is available for Windows, Mac OS X, and Red Hat. We can download it from http://www.enthought.com/products/epd.php.

  • Python(x,y): This package is available for Windows and Ubuntu at http://www.pythonxy.com/.

  • Sage: This package is available for Linux at http://www.sagemath.org/.

These are mainly scientific distributions that install a lot of tools we don't directly need or use, but they have the advantage of making it easy to get Python, NumPy, and Matplotlib installed and working on our system.

Installing Matplotlib from source code

There are two ways of obtaining the Matplotlib source code. They are:

  • Downloading it from the source code tarballs available in the download area of Matplotlib SourceForge project page at http://sourceforge.net/projects/matplotlib/.

  • Retrieving it from the Subversion (SVN) repository. This is the place where development takes place, so use it only if you know what you're doing.

If we decided to go with SVN, we can follow the instructions available in the Develop section of http://sourceforge.net/projects/matplotlib/.

If we are going to use the source code tarball, we will have to unpack it, go into the created source directory, and execute the following commands:

$ python setup.py build
$ sudo python setup.py install

These commands will build and then install Matplotlib. We will need administrative privileges to install it into the system directories (hence the sudo command in this Linux example).

Many aspects of the installation can be tuned using setup.cfg, a file shipped with the source code and used at build and install time. We can use it to customize the build process, such as changing the default backend, or choosing whether to install the optional libraries or not.

If we want to install Matplotlib from source on Windows, the Files section of Matplotlib SourceForge page contains handy egg files which we can download (choosing the Python version of interest) and then install using setuptools command. The following command will install Matplotlib on your machine:

$ easy_install matplotlib-<version>-py<py version>-win32.egg

Egg files are also available for Mac OS X, and we can use them in the same way as described above.

Testing our installation

To ensure we have correctly installed Matplotlib and its dependencies, a very simple test can be carried out in the following manner:

$ python
Python 2.5.4 (r254:67916, Feb 18 2009, 03:00:47)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> print numpy.__version__
1.2.1
>>> import matplotlib
>>> print matplotlib.__version__
0.98.5.3

If there's no error while executing this, then we are done.