# matplotlib - representation of nan values in 2D

Dear matplotlib group,

for the represenation of 2d measurement data I use the contourplot function from matplotlib. Some points in this map are not measurabel, therefore I get a non numerical value (nan) output.

From this data I want to generate a map and a histogram plot. This works fine, as long as I use regular matrix/array data structure without any voids.

My questions are:
(1) How can I make use of plotting data with NAN values as contour and asigns such values eg as black points?

(2) How can I use the matplotlib hist() function with this data, that also include such missing data points?
Maybe there is an easy workaround for this.

Thanks a lot for your support,
Dirk

(python2.51
/matplotlib-0.90.1, win32)

my code:

import matplotlib

my2dData=[[1,2,3,4,5.0 ,NaN,7,8,9,10],[10,9,8,7,6,5,4,3,2,1]]

figure(1)
imshow(my2dData)
pylab.show()

Dirk Zickermann wrote:

Dear matplotlib group,

for the represenation of 2d measurement data I use the contourplot
function from matplotlib. Some points in this map are not measurabel,
therefore I get a non numerical value (nan) output.

From this data I want to generate a map and a histogram plot. This works
fine, as long as I use regular matrix/array data structure without any
voids.

My questions are:
(1) How can I make use of plotting data with NAN values as contour and
asigns such values eg as black points?

I'm not sure what you mean. For a contour plot, you're not assigning
any kind of color to data, but drawing boundaries that enclose regions
with a value greater/less than a certain value. Since, AFAIK,
contouring requires regularly spaced data, you might want to try
interpolating to fill in the missing data before contouring.

If instead, you actually want an image plot (data->colored points), I
thought pcolor at least supported ignoring NaN points.

(2) How can I use the matplotlib hist() function with this data, that
also include such missing data points?
Maybe there is an easy workaround for this.

My solution here would be to create a new 1D array with the NaN points
deleted before calling hist()

Ryan

···

--
Ryan May
School of Meteorology
University of Oklahoma

Hi Dirk,

If you haven’t already done so, look at the numpy.ma
module. It provides a masked array object that deals gracefully with
missing values. To the best of my knowledge, most matplotlib functions
of those requiring a full matrix (such as contour). Take a look at
examples/image_masked.py. Also, in the Basemap toolkit, there is at
least one example showing how to plot a masked array on a map.

Cheers,

David

2007/9/26, Dirk Zickermann <dirk.zickermann@…982…

···

:
Dear matplotlib group,

for the represenation of 2d measurement data I use the contourplot function from matplotlib. Some points in this map are not measurabel, therefore I get a non numerical value (nan) output.

From this data I want to generate a map and a histogram plot. This works fine, as long as I use regular matrix/array data structure without any voids.

My questions are:
(1) How can I make use of plotting data with NAN values as contour and asigns such values eg as black points?

(2) How can I use the matplotlib hist() function with this data, that also include such missing data points?
Maybe there is an easy workaround for this.

Thanks a lot for your support,
Dirk

(python2.51
/matplotlib-0.90.1, win32)

my code:

import matplotlib

my2dData=[[1,2,3,4,5.0 ,NaN,7,8,9,10],[10,9,8,7,6,5,4,3,2,1]]

figure(1)
imshow(my2dData)
pylab.show()

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft® Visual Studio 2005.

http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Matplotlib-users mailing list

David Huard wrote:

Hi Dirk,

If you haven't already done so, look at the numpy.ma <http://numpy.ma/> module. It provides a masked array object that deals gracefully with missing values. To the best of my knowledge, most matplotlib functions understand masked arrays and deal with it accordingly, exception made of those requiring a full matrix (such as contour). Take a look at

contour handles masked arrays correctly, as far as I know; contourf has some bugs in its masked array handling, but depending on the type and distribution of voids, it may still be good enough.

pcolor and image have no problems with masked arrays.

Eric

···

examples/image_masked.py. Also, in the Basemap toolkit, there is at least one example showing how to plot a masked array on a map.

Cheers,

David

Dear all,
thanks for your help. this is what I was looking for!
Dirk

2007/9/26, Eric Firing <efiring@…202…>:

···

David Huard wrote:

Hi Dirk,

If you haven’t already done so, look at the numpy.ma <http://numpy.ma/>
module. It provides a masked array object that deals gracefully with

missing values. To the best of my knowledge, most matplotlib functions
those requiring a full matrix (such as contour). Take a look at

contour handles masked arrays correctly, as far as I know; contourf has
some bugs in its masked array handling, but depending on the type and
distribution of voids, it may still be good enough.

pcolor and image have no problems with masked arrays.

Eric

examples/image_masked.py. Also, in the Basemap toolkit, there is at
least one example showing how to plot a masked array on a map.

Cheers,

David

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft® Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users