Plotting Matlab NaNs in matplotlib

Dear sourceforge community,

I come from a Matlab environment so I am used to plotting matrices that contain NaN elements. This is very useful because in some cases one doesn’t have data for the entire matrix. If one tries plotting the data, the NaN elements won’t be plotted.

Is there a similar element type or workaround I could use to get the same effect?

In the following simple example:

a = [1,2,3,4,5]
b = [6,2,NaN,1,9]
mpylab.plot(a,b)

I would like to get two lines with a gap in between them at element [2].

Thanks for any help you can offer.

Regards,

Fernando

Fernando,

Your example works as you describe on recent matplotlib versions. I suspect you are using an old one. The preferred way of handling missing points in numpy, and therefore in matplotlib and pylab, however, is via masked arrays.

import pylab
import numpy as np
from numpy import ma
a = [1,2,3,4,5]
b = np.array([6,2,np.nan,1,9])
bm = ma.masked_where(np.isnan(b), b)
pylab.plot(a,bm)
pylab.show()

There are many other examples of masked array use in the examples directory of the matplotlib distribution.

Eric

Fernando Abilleira wrote:

···

Dear sourceforge community,

I come from a Matlab environment so I am used to plotting matrices that contain NaN elements. This is very useful because in some cases one doesn't have data for the entire matrix. If one tries plotting the data, the NaN elements won't be plotted.

Is there a similar element type or workaround I could use to get the same effect?

In the following simple example:

a = [1,2,3,4,5]
b = [6,2,NaN,1,9]
mpylab.plot(a,b)

I would like to get two lines with a gap in between them at element [2].

Thanks for any help you can offer.

Regards,

Fernando