Sunday, 11 November 2012

Install matplotlib on Ubuntu 12.10 for python 3

Install matplotlib Ubuntu 12.10

This is a follow-up post for installing matplotlib for Python 3, now that matplotlib 1.2 has been released and officially supports Python 3: it will describe how to install all tools necessary to setup a virtualenvironment, and how to create one that will host the installation of matplotlib and numpy.

Install distribute & pip

Installation instructions for the current version can be found at the Python Package Index.

Normally the following code will suffice:


wget http://python-distribute.org/distribute_setup.py
sudo python3 distribute_setup.py

This will finish installing distribute which will allow us to install pip.


# Careful to use the correct easy_install if you have it installed
# for python2 as well!
sudo easy_install pip

Now you have pip installed and can proceed to install virtualenv and virtualenvwrapper (a number of scripts that make using virtual environments a lot easier).

Install virtualenv & virtualenvwrapper

To install these two packages make sure to use the correct version of pip if you have it installed for Python 2 and Python 3:


sudo pip install virtualenv
sudo pip install virtualenvwrapper

Now you have to make sure virtualenvwrapper is loaded and is using Python 3. To do this just add the following two lines to your bashrc (or zshrc) file:


VIRTUALENVWRAPPER_PYTHON='/usr/bin/python3'
source /usr/local/bin/virtualenvwrapper.sh

Installing matplotlib

First create a new virtual environment for matplotlib:


mkvirtualenv <name_of_environment>

The virtual environment will normally be automatically activated (you can normally see this when the shell prompt is prepended with (<name_of_environment>)<rest_of_your_prompt>:).

If it is not activated simply issue the following command:


workon <name_of_environment>

Now before installing matplotlib we need to install a few more dependencies: to install numpy we need Python header files and to compile numpy we need a c-compiler.

To be able to display plots using the TkAgg back-end we will also need the Tk-libraries, as well as the tk-headers and libpng headers.

All these dependencies can be installed with apt-get:


sudo apt-get install python3-dev build-essential python3-tk tk-dev libpng12-dev

Then you can install the dependencies for matplotlib and matplotlib itself:


pip install numpy
# using <package>==<version> syntax makes sure to install exactly the
# specified version.
pip install matplotlib==1.2

This should allow you to run the following script and produce a simple linear graph:


import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()

No comments:

Post a Comment