• Facebook
  • Twitter
  • Reddit
  • StumbleUpon
  • Digg
  • email

All Samples(170)  |  Call(0)  |  Derive(0)  |  Import(170)
=============
Masked Arrays
=============

Arrays sometimes contain invalid or missing data.  When doing operations
on such arrays, we wish to suppress invalid values, which is the purpose masked
arrays fulfill (an example of typical use is given below).

For example, examine the following array:

>>> x = np.array([2, 1, 3, np.nan, 5, 2, 3, np.nan])

When we try to calculate the mean of the data, the result is undetermined:

>>> np.mean(x)
nan

The mean is calculated using roughly ``np.sum(x)/len(x)``, but since
any number added to ``NaN`` [1]_ produces ``NaN``, this doesn't work.  Enter
masked arrays:

>>> m = np.ma.masked_array(x, np.isnan(x))
>>> m
masked_array(data = [2.0 1.0 3.0 -- 5.0 2.0 3.0 --],
      mask = [False False False  True False False False  True],
      fill_value=1e+20)

Here, we construct a masked array that suppress all ``NaN`` values.  We
may now proceed to calculate the mean of the other values:

>>> np.mean(m)
2.6666666666666665

.. [1] Not-a-Number, a floating point value that is the result of an
       invalid operation.

src/m/a/matplotlib-HEAD/matplotlib/examples/pylab_examples/quadmesh_demo.py   matplotlib(Download)
import numpy as np
from matplotlib.pyplot import figure, show, savefig
from matplotlib import cm, colors
from numpy import ma
 
n = 12
x = np.linspace(-1.5,1.5,n)

src/m/a/matplotlib-HEAD/examples/pylab_examples/quadmesh_demo.py   matplotlib(Download)
import numpy as np
from matplotlib.pyplot import figure, show, savefig
from matplotlib import cm, colors
from numpy import ma
 
n = 12
x = np.linspace(-1.5,1.5,n)

src/m/a/Matplotlib--JJ-s-dev-HEAD/examples/pylab_examples/quadmesh_demo.py   Matplotlib--JJ-s-dev(Download)
import numpy as np
from matplotlib.pyplot import figure, show, savefig
from matplotlib import cm, colors
from numpy import ma
 
n = 12
x = np.linspace(-1.5,1.5,n)

src/m/a/matplotlib-HEAD/matplotlib/examples/pylab_examples/color_by_yvalue.py   matplotlib(Download)
# use masked arrays to plot a line with different colors by y-value
from numpy import logical_or, arange, sin, pi
from numpy import ma
from matplotlib.pyplot import  plot, show
 
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)

src/m/a/matplotlib-HEAD/examples/pylab_examples/color_by_yvalue.py   matplotlib(Download)
# use masked arrays to plot a line with different colors by y-value
from numpy import logical_or, arange, sin, pi
from numpy import ma
from matplotlib.pyplot import  plot, show
 
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)

src/m/a/Matplotlib--JJ-s-dev-HEAD/examples/pylab_examples/color_by_yvalue.py   Matplotlib--JJ-s-dev(Download)
# use masked arrays to plot a line with different colors by y-value
from numpy import logical_or, arange, sin, pi
from numpy import ma
from matplotlib.pyplot import  plot, show
 
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)

src/m/a/matplotlib-HEAD/toolkits/basemap/examples/ccsm_popgrid.py   matplotlib(Download)
POP grids are used extensively locally in oceanographic and ice models.
"""
from matplotlib import rcParams
import numpy.ma as ma
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, NetCDFFile

src/m/a/matplotlib-HEAD/matplotlib/examples/pylab_examples/contourf_log.py   matplotlib(Download)
'''
Demonstrate use of a log color scale in contourf
'''
 
from matplotlib import pyplot as P
import numpy as np
from numpy import ma

src/m/a/matplotlib-HEAD/examples/pylab_examples/contourf_log.py   matplotlib(Download)
'''
Demonstrate use of a log color scale in contourf
'''
 
from matplotlib import pyplot as P
import numpy as np
from numpy import ma

src/m/a/matplotlib-HEAD/matplotlib/examples/pylab_examples/step_demo.py   matplotlib(Download)
import numpy as np
from numpy import ma
from matplotlib.pyplot import step, legend, xlim, ylim, show
 
x = np.arange(1, 7, 0.4)
y0 = np.sin(x)
y = y0.copy() + 2.5

  1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9  Next