## Re: [Matplotlib-users] plotting with missing data?

 Re: [Matplotlib-users] plotting with missing data? From: Eric Firing - 2008-03-18 20:17:42 ```Chris Withers wrote: > Eric Firing wrote: >>> Specifically, what I have is an array like so: >>> >>> ['','','',1.1,2.2] >> >> Try something like this: >> >> import numpy.ma as ma >> from pylab import * >> >> aa = [3.4, 2.5, '','','',1.1,2.2] >> def to_num(arg): >> if arg == '': >> return 9999.0 >> return arg >> >> aanum = array([to_num(arg) for arg in aa]) >> aamasked = ma.masked_where(aanum==9999.0, aanum) >> plot(aamasked) >> show() > > What I ended up doing was getting my array to look like: > > from numpy import nan > aa = [3.4,2.5,nan,nan,nan,1.1,2.2] > values = numpy.array(aa) > values = numpy.ma.masked_equal(values,nan) This is not doing what you think it is, because any logical operation with a Nan returns False: In [4]:nan == nan Out[4]:False You should use numpy.masked_where(numpy.isnan(aa), aa). In some places in mpl, nans are treated as missing values, but this is not uniformly true, so it is better not to count on it. Your values array is not actually getting masked at the nans: In [7]:aa = array([1,nan,2]) In [8]:aa Out[8]:array([ 1., NaN, 2.]) In [9]:values = ma.masked_equal(aa, nan) In [10]:values Out[10]: masked_array(data = [1.0 nan 2.0], mask = [False False False], fill_value=1e+20) Eric > > I only wish that masked_equal didn't blow up when aa contains datetime > objects :-( > > cheers, > > Chris > ```