pcolor / Non-Uniform Images


I've recently come across a need to plot images for which I have
irregular sample points. As far as I can see the way to do this in
current mpl CVS is either pcolor or contourf (which is sometimes much

I've implemented a third way with a subclass of AxisImage called
NonUniformImage which creates an axes image using a custom extension to
the PyCXX _image module. The NonUniformImage class first turns all data
to a MxNx4 UInt8 on initialisation as a cache. The make_image function
is replaced to call the extension code on each call with the x and y
axes, the RGBA image data, the size of the image to output and the view
limits. This code uses nearest neighbour interpolation to determine the
closest colour and create the output. By putting the heavy calculations
into C++, by avoiding dealing with sample points that aren't rendered
and by only calculating the sample point to pixel map once per call this
code allows easy viewing and scrolling on fairly high resolution data.
On my laptop (1GB memory) 2.56 million points are handled fairly easily
(test script below:

I've attached a patch to CVS with the necessary changes below. There
are some issues here:

- I'm not sure what the axes.Axes function to access this should be
called so I haven't made one.

- I'm not sure how to handle image boundaries; I currently have no
boundaries and just choose the nearest sample point - however far away
that is.

- To cope with large images the original array data is not stored and
thus cmap and norm cannot be changed once set_data has been called.

test code:

mpl_nui.patch (9.45 KB)



from pylab import *
from Numeric import NewAxis
from matplotlib.image import NonUniformImage

x = arange(-4, 4, 0.005)
y = arange(-4, 4, 0.005)
print 'Size %d points' % (len(x) * len(y))
z = sqrt(x[NewAxis,:]**2 + y[:,NewAxis]**2)

im = NonUniformImage(gca())
im.set_data(x, y, z)

x2 = x**3

im = NonUniformImage(gca())
im.set_data(x2, y, z)


Hope this is useful,

My patch contained memory leaks which I've fixed in the attachment - but
I'm not that experienced in c/c++ so there could be more I haven't


mpl_nui.patch (9.74 KB)


On Wed, 2005-09-07 at 16:15 +0100, Nicholas Young wrote:

I've attached a patch to CVS with the necessary changes below. There
are some issues here: