Howto display an array as an image (like imagesc in matlab)

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Jul 29 14:25:02 EDT 2004


>>>>> "Mathias" == Mathias  <no_sp at m_please.cc> writes:

    Mathias> Dear NG, I currently ty to switch from matlab to
    Mathias> python/scipy but have a lot of trouble with images. What

matplotlib (by yours truly) is designed as a matlab compatible
plotting library for python.  It has 'imshow' to display images.  You
can manage several figures, axes, and images as in matlab with the
matlab compatible commands figure / subplot / axes / gcf / gca / close
/ clf / cla / hold.

  http://matplotlib.sf.net

Here is a simple script to create and display an image

    from matplotlib.matlab import *

    delta = 0.025
    x = y = arange(-3.0, 3.0, delta)
    X, Y = meshgrid(x, y)
    Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
    Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)

    # difference of Gaussians
    im = imshow(Z2-Z1, interpolation='bilinear')
    axis('off')
    show()

Additional matlab compatible functions for color mapping are provided
(colorbar, jet, gray) and a variety of interpolation methods are
available (nearest neighbor, bilinear, bicubic, much more).

In addition, it supports features I don't believe matlab has, like the
ability to automatically blend multiple images with different alpha
values while 'hold' is on - see for example
http://matplotlib.sourceforge.net/screenshots.html#layer_images

Outputs to png, ps, svg (image support for svg only in CVS).  Also in
CVS is imread to load images into arrays. 

If you are very familiar with matlab, matplotlib will be a breeze -
there is a brief tutorial at
http://matplotlib.sourceforge.net/tutorial.html

On win32, if you are using the enthought edition of python
(recommended!), you can use matplotlib out of the box with either the
tkinter or wxpython backends.

JDH




More information about the Python-list mailing list