matplotlib – plotting with Python

July 26th, 2008 at 12:58 pm

matplotlib is a:

python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala matlab or mathematica), web application servers, and six graphical user interface toolkits.

The plots produced by maplotlib really are very pretty, on par with the plots generated by Matlab.

Histogram made with matplotlib

Installing matplotlib on Windows was a breeze. All you need is the .exe installer from their website, plus the .exe installer of NumPy – a scientific computing package used by matplotlib. That, in combination with a more advanced package named SciPy creates a quite serious contender to the title of “open-source Matlab replacement”.

Unfortunately, the documentation for numpy and matplotlib isn’t very comprehensive, and some of it is outdated. So the best reference is probably the source code documentation, which can be browsed online.

Some random observations from a couple of hours of toying with matplotlib:

  • maplotlib is very suitable for interactive plotting, just like Matlab. The best support for this is with the IPython shell, but it is possible to make it work with PyCrust as well. This worked for me:
    
    >>> import matplotlib
    >>> matplotlib.use('WXAgg')
    >>> matplotlib.interactive(True)
    >>> from pylab import *
    >>> plot([1, 2, 3])
    
    

    Note the directive to use the WXAgg backend. The WX backend didn’t work for me, it throws an error when run from PyCrust.

  • The Windows .exe distribution doesn’t come with the examples directory, so make sure to download it separately – it’s very helpful.
  • I’m using wxPython for the GUI, so naturally I’m interested in embedding plots into wxPython applications. matplotlib has some examples of how to do this. I found the wxcursor_demo and printing_in_wx examples in the user_interfaces/ directory most helpful. There seems to be a general problem with the WX backend of matplotlib, but WXAgg seems to work well.
  • One of the best features of matplotlib is the interaction with the plots. There are some great demos in the widgets/ examples directory, and I wish there were more documentation on this topic, because it can make for some really great features when integrated into a GUI.

Related posts:

  1. Plotting in Python: matplotlib vs. PyQwt
  2. Book review: “Matplotlib for Python developers” by Sandro Tosi
  3. matplotlib with PyQt GUIs
  4. Python
  5. matplotlib with wxPython GUIs

4 Responses to “matplotlib – plotting with Python”

  1. Ryan DaleNo Gravatar Says:

    I’ve recently made the complete switch from Matlab to Python, and matplotlib played a large part in that switch. Some documentation links that have helped me along the way, in case you haven’t seen them:

    up-to-date, Sphinx-generated matplotlib docs here:
    http://matplotlib.sourceforge.net/doc/html/users/index.html

    and especially here for the interaction side of things:
    http://matplotlib.sourceforge.net/doc/html/users/event_handling.html

    For a NumPy quick overview:
    http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python/arrays.html

    and more comprehensive examples:
    http://www.scipy.org/Numpy_Example_List_With_Doc

    For translating between Matlab, R, Python, et al:
    http://mathesaurus.sourceforge.net/

    -ryan

  2. KWNo Gravatar Says:

    If you need to graphs that are out of the ordinary, for example you need curved axis, triangular graphs, and so on, take a look at SVGfig (http://code.google.com/p/svgfig/).

  3. NoyanNo Gravatar Says:

    I’am new user on python.I have problem import matplotlib.Can you help me Please ? What’s wrong I do ? Error is below:

    ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
    Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
    Type “copyright”, “credits” or “license()” for more information.
    >>> import matplotlib

    Traceback (most recent call last):
    File “”, line 1, in
    import matplotlib
    File “C:\Python27\lib\site-packages\matplotlib\__init__.py”, line 133, in
    from matplotlib.rcsetup import (defaultParams,
    File “C:\Python27\lib\site-packages\matplotlib\rcsetup.py”, line 19, in
    from matplotlib.colors import is_color_like
    File “C:\Python27\lib\site-packages\matplotlib\colors.py”, line 52, in
    import numpy as np
    ImportError: No module named numpy
    >>>

  4. elibenNo Gravatar Says:

    Noyan,

    The numpy package is a pre-requisite for matplotlib, so you have to install it first.