## Re: [Matplotlib-users] Re: plot with nans

 Re: [Matplotlib-users] Re: plot with nans From: Alan G Isaac - 2005-07-13 16:16:11 ```On Tue, 12 Jul 2005, Eric Firing apparently wrote: >>> import numarray as N >>> from numarray.ieeespecial import isnan, nan >>> import numarray.ma as ma >>> a = N.array([1, 2, nan, 3, 4]) >>> b = ma.masked_where(isnan(a), a) > Now you can plot b and it will have a gap in the middle. Thanks. Alan ```

 [Matplotlib-users] Re: plot with nans From: Eric Firing - 2005-07-12 08:07:01 ```Alan, > > 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. Slightly modifying a reply to a similar question not long ago: At present, you need to use masked arrays instead of NaN. NaN is not uniformly supported in Python and numerix, but masked arrays, in both numarray and Numeric, can accomplish the same thing and more. See masked_demo.py in the mpl examples subdirectory, and see http://stsdas.stsci.edu/numarray/numarray-1.3.html/module-numarray.ma.html for an explanation of masked arrays. Numeric is in the midst of a major revision (including renaming), which I think will include support of NaNs similar to that in numarray. When this new version becomes common, then it may be both possible and desirable for matplotlib to handle NaNs as smoothly as Matlab, Octave, and evidently Gauss do. Eric ```
 [Matplotlib-users] Re: plot with nans From: Mark Bakker - 2005-07-12 17:04:00 ```Plotting with 'nans' doesn't work as far as I know. Read the discussion on using masked arrays posted in the last 2 weeks. They work well in my experience, Mark ```
 Re: [Matplotlib-users] Re: plot with nans From: Darren Dale - 2005-07-12 17:17:19 ```On Tuesday 12 July 2005 01:03 pm, Mark Bakker wrote: > Plotting with 'nans' doesn't work as far as I know. > Read the discussion on using masked arrays posted in the last 2 weeks. I had asked about plotting with NaNs a while back. John says the problem is with the way the Agg backend deals with them. plot([0,nan,2,3],'o') works, plot([nan,1,2,3],'o') does not. -- Darren ```
 [Matplotlib-users] Re: plot with nans From: Eric Firing - 2005-07-13 07:35:01 ```Alan, > > But I still feel something is missing from this discussion. > Would it be odd/hard for plot() to have an option that says, > effectively, mask NaNs so that the expected behavior results? Again, the problem is that automatic masking of nans would be easy with numarray but not at all easy with Numeric. Given that mpl is designed to work the same with numarray and Numeric, it is not easy to build good nan support into mpl. This will change when the successor to Numeric becomes common enough that support for old Numeric can be dropped, but not soon. In the meantime, if you want to use nans, you can do something like this: >>> import numarray as N >>> from numarray.ieeespecial import isnan, nan >>> import numarray.ma as ma >>> a = N.array([1, 2, nan, 3, 4]) >>> b = ma.masked_where(isnan(a), a) Now you can plot b and it will have a gap in the middle. Eric ```
 Re: [Matplotlib-users] Re: plot with nans From: Alan G Isaac - 2005-07-13 16:16:11 ```On Tue, 12 Jul 2005, Eric Firing apparently wrote: >>> import numarray as N >>> from numarray.ieeespecial import isnan, nan >>> import numarray.ma as ma >>> a = N.array([1, 2, nan, 3, 4]) >>> b = ma.masked_where(isnan(a), a) > Now you can plot b and it will have a gap in the middle. Thanks. Alan ```
 Re: [Matplotlib-users] Re: plot with nans From: Christian Kristukat - 2005-07-14 09:03:14 ```Hi Eric, Eric Firing wrote: > Again, the problem is that automatic masking of nans would be easy with > numarray but not at all easy with Numeric. Given that mpl is designed > to work the same with numarray and Numeric, it is not easy to build good > nan support into mpl. This will change when the successor to Numeric > becomes common enough that support for old Numeric can be dropped, but > not soon. In the meantime, if you want to use nans, you can do > something like this: > > >>> import numarray as N > >>> from numarray.ieeespecial import isnan, nan > >>> import numarray.ma as ma > >>> a = N.array([1, 2, nan, 3, 4]) > >>> b = ma.masked_where(isnan(a), a) > > Now you can plot b and it will have a gap in the middle. > I tried it as described above but plotting with pylab.plot results in an error message: MAError: Cannot automatically convert masked array to Numeric because data is masked in one or more locations. Am I missing something? Regards, Christian ```
 [Matplotlib-users] Re: plot with nans From: Eric Firing - 2005-07-14 21:10:26 ```Christian, >> >> >>> import numarray as N >> >>> from numarray.ieeespecial import isnan, nan >> >>> import numarray.ma as ma >> >>> a = N.array([1, 2, nan, 3, 4]) >> >>> b = ma.masked_where(isnan(a), a) >> >> Now you can plot b and it will have a gap in the middle. >> > > > I tried it as described above but plotting with pylab.plot results in an > error message: > > MAError: Cannot automatically convert masked array to Numeric because data > is masked in one or more locations. > > Am I missing something? > > Regards, Christian > The problem is that you need to use numarray, not Numeric, as your numerix choice. In your matplotlibrc file, use numerix : numarray # Numeric or numarray instead of the default, which is Numeric. Unfortunately, although there is some partial compatibility between Numeric and numarray, it does not extend to one being able to read masked arrays from the other, so you need to use one or the other consistently. And if you want to work with nans, then numarray is the one you need to use. Eric ```
 [Matplotlib-users] Re[2]: plot with nans From: Alan G Isaac - 2005-07-13 00:23:50 ```>> x=[1,2,nan,4,5] plot(x) plots a single line. >> I expect two segments and a gap. On Mon, 11 Jul 2005, Eric Firing apparently wrote: > Slightly modifying a reply to a similar question not long ago: > At present, you need to use masked arrays instead of NaN. So if I use masked arrays, I should get two segments and a gap in the above case? I'm not familiar with masked arrays. Sounds like I'll have to learn about them. But I still feel something is missing from this discussion. Would it be odd/hard for plot() to have an option that says, effectively, mask NaNs so that the expected behavior results? Thanks, Alan ```