how to plot contour on a regular grid with mask ?

Hi

   I have a function z(x, y) on a regular grid. But some of the value
z are not defined on (x,y). I want to plot the contour or contourf of
z on (x,y) but exclude specific (x,y) points.
How can I do it ? Right now I just draw small colored square
(rectangular) around defined (x,y) the color is not smooth since no
interpolation like contour or contourf.

Thanks.

Forest.

Forest,

There are a few ways to do this. If you have a recent enough version of matplotlib, you can use masked arrays, and the contourf will just ignore those data points. One could also use NaNs and make sure that the clim (the limits on z that you wish to display a color for) is defined.

To make a masked array is easy. Imagine you wish to exclude any value less than zero (assume z is defined):

import numpy.ma as ma
z_masked = ma.masked_array(z, mask=(z < 0.))

And then just use the masked array in your contourf as you would the regular numpy array.

I hope that helps!
Ben Root

···

On Fri, Sep 17, 2010 at 8:05 PM, Forest Yang <yzine0511@…985…> wrote:

Hi

I have a function z(x, y) on a regular grid. But some of the value

z are not defined on (x,y). I want to plot the contour or contourf of

z on (x,y) but exclude specific (x,y) points.

How can I do it ? Right now I just draw small colored square

(rectangular) around defined (x,y) the color is not smooth since no

interpolation like contour or contourf.

Thanks.

Forest.

Thanks, that works fantastically !

-- Forest.

···

On Fri, Sep 17, 2010 at 9:23 PM, Benjamin Root <ben.root@...1304...> wrote:

On Fri, Sep 17, 2010 at 8:05 PM, Forest Yang <yzine0511@...287...> wrote:

Hi

I have a function z(x, y) on a regular grid. But some of the value
z are not defined on (x,y). I want to plot the contour or contourf of
z on (x,y) but exclude specific (x,y) points.
How can I do it ? Right now I just draw small colored square
(rectangular) around defined (x,y) the color is not smooth since no
interpolation like contour or contourf.

Thanks.

Forest.

Forest,

There are a few ways to do this. If you have a recent enough version of
matplotlib, you can use masked arrays, and the contourf will just ignore
those data points. One could also use NaNs and make sure that the clim (the
limits on z that you wish to display a color for) is defined.

To make a masked array is easy. Imagine you wish to exclude any value less
than zero (assume z is defined):

import numpy.ma as ma
z_masked = ma.masked_array(z, mask=(z < 0.))

And then just use the masked array in your contourf as you would the
regular numpy array.

I hope that helps!
Ben Root