Contents
See Report a problem.
The first thing to try is a clean install and see if that helps. If not, the best way to test your install is by running a script, rather than working interactively from a python shell or an integrated development environment such as IDLE which add additional complexities. Open up a UNIX shell or a DOS command prompt and cd into a directory containing a minimal example in a file. Something like simple_plot.py, or for example:
from pylab import *
plot([1,2,3])
show()
and run it with:
python simple_plot.py --verbose-helpful
This will give you additional information about which backends matplotlib is loading, version information, and more. At this point you might want to make sure you understand matplotlib’s configuration process, governed by the matplotlibrc configuration file which contains instructions within and the concept of the matplotlib backend.
If you are still having trouble, see Report a problem.
The steps depend on your platform and installation method.
Delete the caches from your .matplotlib configuration directory.
Run:
easy_install -m PackageName
Delete any .egg files or directories from your installation directory.
Unfortunately:
python setup.py clean
does not properly clean the build directory, and does nothing to the install directory. To cleanly rebuild:
Checking out the main source:
svn co https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/trunk/matplotlib matplotlib
and build and install as usual with:
> cd matplotlib
> python setup.py install
If you want to be able to follow the development branch as it changes just replace the last step with (Make sure you have setuptools installed):
> python setupegg.py develop
This creates links in the right places and installs the command line script to the appropriate places. Then, if you want to update your matplotlib at any time, just do:
> svn update
When you run svn update, if the output shows that only Python files have been updated, you are all set. If C files have changed, you need to run the python setupegg develop command again to compile them.
There is more information on using Subversion in the developer docs.
A lot of documentation on the website and in the mailing lists refers to the “backend” and many new users are confused by this term. matplotlib targets many different use cases and output formats. Some people use matplotlib interactively from the python shell and have plotting windows pop up when they type commands. Some people embed matplotlib into graphical user interfaces like wxpython or pygtk to build rich applications. Others use matplotlib in batch scripts to generate postscript images from some numerical simulations, and still others in web application servers to dynamically serve up graphs.
To support all of these use cases, matplotlib can target different outputs, and each of these capabililities is called a backend; the “frontend” is the user facing code, ie the plotting code, whereas the “backend” does all the dirty work behind the scenes to make the figure. There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt, macosx, or fltk) and hardcopy backends to make image files (PNG, SVG, PDF, PS).
There are a two primary ways to configure your backend. One is to set the backend parameter in you matplotlibrc file (see Customizing matplotlib):
backend : WXAgg # use wxpython with antigrain (agg) rendering
The other is to use the matplotlib use() directive:
import matplotlib
matplotlib.use('PS') # generate postscript output by default
If you use the use directive, this must be done before importing matplotlib.pyplot or matplotlib.pylab.
If you are unsure what to do, and just want to get cranking, just set your backend to TkAgg. This will do the right thing for 95% of the users. It gives you the option of running your scripts in batch or working interactively from the python shell, with the least amount of hassles, and is smart enough to do the right thing when you ask for postscript, or pdf, or other image formats.
If however, you want to write graphical user interfaces, or a web application server (Matplotlib in a web application server), or need a better understanding of what is going on, read on. To make things a little more customizable for graphical user interfaces, matplotlib separates the concept of the renderer (the thing that actually does the drawing) from the canvas (the place where the drawing goes). The canonical renderer for user interfaces is Agg which uses the antigrain C++ library to make a raster (pixel) image of the figure. All of the user interfaces can be used with agg rendering, eg WXAgg, GTKAgg, QTAgg, TkAgg, CocoaAgg. In addition, some of the user interfaces support other rendering engines. For example, with GTK, you can also select GDK rendering (backend GTK) or Cairo rendering (backend GTKCairo).
For the rendering engines, one can also distinguish between vector or raster renderers. Vector graphics languages issue drawing commands like “draw a line from this point to this point” and hence are scale free, and raster backends generate a pixel represenation of the line whose accuracy depends on a DPI setting.
Here is a summary of the matplotlib renderers (there is an eponymous backed for each):
Renderer | Filetypes | Description |
---|---|---|
AGG | png | raster graphics – high quality images using the Anti-Grain Geometry engine |
PS | ps eps | vector graphics – Postscript output |
vector graphics – Portable Document Format | ||
SVG | svg | vector graphics – Scalable Vector Graphics |
Cairo | png ps pdf svg ... | vector graphics – Cairo graphics |
GDK | png jpg tiff ... | raster graphics – the Gimp Drawing Kit |
And here are the user interfaces and renderer combinations supported:
Backend | Description |
---|---|
GTKAgg | Agg rendering to a GTK canvas (requires PyGTK) |
GTK | GDK rendering to a GTK canvas (not recommended) (requires PyGTK) |
GTKCairo | Cairo rendering to a GTK Canvas (requires PyGTK) |
WXAgg | Agg rendering to to a wxWidgets canvas (requires wxPython) |
WX | Native wxWidgets drawing to a wxWidgets Canvas (not recommended) (requires wxPython) |
TkAgg | Agg rendering to a Tk canvas (requires TkInter) |
QtAgg | Agg rendering to a Qt canvas (requires PyQt) |
Qt4Agg | Agg rendering to a Qt4 canvas (requires PyQt4) |
FLTKAgg | Agg rendering to a FLTK canvas (requires pyFLTK) |
macosx | Cocoa rendering in OSX windows |
There is a bug in PyGTK-2.4. You need to edit pygobject.h to add the G_BEGIN_DECLS and G_END_DECLS macros, and rename typename parameter to typename_:
- const char *typename,
+ const char *typename_,
Apple ships with its own python, many users have had trouble with it so there are alternatives. If it is feasible for you, we recommend the enthought python distribution EPD for OS X (which comes with matplotlib and much more) or the MacPython or the official OS X version from python.org.
If you want to install matplotlib from one of the binary installers we build, you have two choices: a mpkg installer, which is a typical Installer.app, or an binary OSX egg, which you can install via setuptools easy_install.
The mkpg installer will have a “zip” extension, and will have a name like file:matplotlib-0.99.0.rc1-py2.5-macosx10.5_mpkg.zip depending on the python, matplotlib, and OSX versions. You need to unzip this file using either the “unzip” command on OSX, or simply double clicking on it to run StuffIt Expander. When you double click on the resultant mpkd directory, which will have a name like file:matplotlib-0.99.0.rc1-py2.5-macosx10.5.mpkg, it will run the Installer.app, prompt you for a password if you need system wide installation privileges, and install to a directory like file:/Library/Python/2.5/site-packages/, again depedending on your python version. This directory may not be in your python path, so you can test your installation with:
> python -c 'import matplotlib; print matplotlib.__version__, matplotlib.__file__'
If you get an error like:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named matplotlib
then you will need to set your PYTHONPATH, eg:
export PYTHONPATH=/Library/Python/2.5/site-packages:$PYTHONPATH
See also ref:environment-variables.
You can also us the eggs we build for OSX (see the installation instructions for easy_install if you do not have it on your system already). You can try:
> easy_install matplotlib
which should grab the latest egg from the sourceforge site, but the naming conventions for OSX eggs appear to be broken (see below) so there is no guarantee the right egg will be found. We recommend you download the latest egg from our download site directly to your harddrive, and manually install it with
> easy_install –install-dir=~/dev/lib/python2.5/site-packages/ matplotlib-0.99.0.rc1-py2.5-macosx-10.5-i386.egg
Some users have reported problems with the egg for 0.98 from the matplotlib download site, with easy_install, getting an error:
> easy_install ./matplotlib-0.98.0-py2.5-macosx-10.3-fat.egg
Processing matplotlib-0.98.0-py2.5-macosx-10.3-fat.egg
removing '/Library/Python/2.5/site-packages/matplotlib-0.98.0-py2.5-
...snip...
Reading http://matplotlib.sourceforge.net
Reading http://cheeseshop.python.org/pypi/matplotlib/0.91.3
No local packages or download links found for matplotlib==0.98.0
error: Could not find suitable distribution for
Requirement.parse('matplotlib==0.98.0')
If you rename matplotlib-0.98.0-py2.5-macosx-10.3-fat.egg to matplotlib-0.98.0-py2.5.egg, easy_install will install it from the disk. Many Mac OS X eggs with cruft at the end of the filename, which prevents their installation through easy_install. Renaming is all it takes to install them; still, it’s annoying.
If you have the EPD installed (Which python for OS X?), it might turn out to be rather tricky to install a new version of matplotlib from source on the Mac OS 10.5 . Here’s a procedure that seems to work, at least sometimes:
1. Edit the file (make a backup before you start, just in case): /Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/config/Makefile, removing all occurrences of the string -arch ppc, changing the line MACOSX_DEPLOYMENT_TARGET=10.3 to MACOSX_DEPLOYMENT_TARGET=10.5 and changing the occurrences of MacOSX10.4u.sdk into MacOSX10.5.sdk
2. In /Library/Frameworks/Python.framework/Versions/Current/lib/pythonX.Y/site-packages/easy-install.pth, (where X.Y is the version of Python you are building against) Comment out the line containing the name of the directory in which the previous version of MPL was installed (Looks something like ./matplotlib-0.98.5.2n2-py2.5-macosx-10.3-fat.egg).
Save the following as a shell script , for example ./install-matplotlib-epd-osx.sh
NAME=matplotlib
VERSION=0_99
PREFIX=$HOME
#branch="release"
branch="trunk"
if [ $branch = "trunk" ]
then
echo getting the trunk
svn co https://matplotlib.svn.sourceforge.net/svnroot/$NAME/trunk/$NAME $NAME
cd $NAME
fi
if [ $branch = "release" ]
then
echo getting the maintenance branch
svn co https://matplotlib.svn.sf.net/svnroot/matplotlib/branches/v${VERSION}_maint $NAME$VERSION
cd $NAME$VERSION
fi
export CFLAGS="-Os -arch i386"
export LDFLAGS="-Os -arch i386"
export PKG_CONFIG_PATH="/usr/x11/lib/pkgconfig"
export ARCHFLAGS="-arch i386"
python setup.py build
python setup.py install #--prefix=$PREFIX #Use this if you don't want it installed into your default location
cd ..
Run this script (for example sh ./install-matplotlib-epd-osx.sh) in the directory in which you want the source code to be placed, or simply type the commands in the terminal command line. This script sets some local variable (CFLAGS, LDFLAGS, PKG_CONFIG_PATH, ARCHFLAGS), removes previous installations, checks out the source from svn, builds and installs it. The backend seems to be set to MacOSX.
If you have already installed python, you can use one of the matplotlib binary installers for windows – you can get these from the sourceforge download site. Choose the files that match your version of python (eg py2.5 if you installed Python 2.5) which have the exe extension. If you haven’t already installed python, you can get the official version from the python web site. There are also two packaged distributions of python that come preloaded with matplotlib and many other tools like ipython, numpy, scipy, vtk and user interface toolkits. These packages are quite large because they come with so much, but you get everything with a single click installer.