plot with nans

Is the default handling of nan by plot the "right" one?
I am accustomed (from GAUSS) to missing values being
treated as "gaps".
E.g.,
    x=[nan,2,3,4,5]
    plot(x)
fails completely. I expect the last 4 numbers to be
plotted.
    x=[1,2,nan,4,5]
    plot(x)
plots a single line. I expect two segments and a gap.

fwiw,
Alan Isaac

Hi Alan,

It seems MaskedArrays are the preferred way to do this. See the masked_demo.py example.

And while I'm at it, does anyone if there is simple function call which returns only the unmasked values of a masked array (without filling the masked values in, but simply returning a shorter sequence)? What is it? I have my own, but it's not pretty:

import matplotlib.numerix as nx
import matplotlib.numerix.ma as M

def unmasked_values(b):
     indices = nx.where(b.mask()==0)
     result = M.take(b,indices).filled()[0]
     return result

(tested only in numarray)

Cheers!
Andrew

···

On Jul 11, 2005, at 6:21 PM, Alan G Isaac wrote:

Is the default handling of nan by plot the "right" one?
I am accustomed (from GAUSS) to missing values being
treated as "gaps".
E.g.,
    x=[nan,2,3,4,5]
    plot(x)
fails completely. I expect the last 4 numbers to be
plotted.
    x=[1,2,nan,4,5]
    plot(x)
plots a single line. I expect two segments and a gap.

fwiw,
Alan Isaac

-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP,
AMD, and NVIDIA. To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Andrew Straw wrote:

It seems MaskedArrays are the preferred way to do this. See the
masked_demo.py example.

Why is that? Couldn't both be handled in the same way?

The only advantage of MaskedArray is the portability to non IEEE
systems. However, 99% of the systems in use are IEEE conformant, and
there NaN is by far more powerful.

Andrew Straw wrote:

Hi Alan,

It seems MaskedArrays are the preferred way to do this. See the masked_demo.py example.

And while I'm at it, does anyone if there is simple function call which returns only the unmasked values of a masked array (without filling the masked values in, but simply returning a shorter sequence)? What is it? I have my own, but it's not pretty:

Andrew:

If x is a masked array, x.compressed() returns the unmasked contented in a ravelled (i.e. rank-1) array. For example:

>>> x = ma.array(arange(5), mask = [0,0,1,0,0])
>>> print x.compressed()
[0,1,3,4,]

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/CDC1 FAX : (303)497-6449
325 Broadway Web : http://www.cdc.noaa.gov/~jsw
Boulder, CO, USA 80305-3328 Office: Skaggs Research Cntr 1D-124

Norbert Nemec wrote:

Andrew Straw wrote:

It seems MaskedArrays are the preferred way to do this. See the
masked_demo.py example.

Why is that? Couldn't both be handled in the same way?

The only advantage of MaskedArray is the portability to non IEEE
systems. However, 99% of the systems in use are IEEE conformant, and
there NaN is by far more powerful.

The reason is compatibility across platforms, and the issue is (at
least) at the level of the Python interpreter. Handling of nans and infs
depends on the hardware-specific representation and C library support.
Have you ever tried struct.pack('>f',nan)?

Maybe someday these issues will get resolved in a cross-platform manner.
Or maybe not. For now, masked arrays are the way to go.

A couple threads of interest:

http://groups-beta.google.com/group/comp.lang.python/msg/16dbf848c050405a

Cheers!
Andrew