There is numpy.ma.masked_where
for masking a a single value. And there is also numpy.ma.masked_inside
for masking intervals.
However I don't quite understand how it is supposed to work.
import numpy.ma as M
from pylab import *
figure()
xx = np.arange(-0.5,5.5,0.01)
vals = 1/(xx-2)
vals = M.array(vals)
mvals = M.masked_where(xx==2, vals)
subplot(121)
plot(xx, mvals, linewidth=3, color='red')
xlim(-1,6)
ylim(-5,5)
However, I would like to do something like this (Which does not work, I know):
mvals = M.masked_where(abs(xx) < 2.001 and abs(xx) > 1.999, vals)
Therefore I tried to use masked_inside
like this :
mvals = ma.masked_inside(xx, 1.999, 2.001)
But the result is not what I wanted, it's just a straight line... I wanted something like this.
The entire script is this:
def f(x):
return (x**3 - 3*x) / (x**2 - 4)
figure()
xx = np.arange(begin, end, precision)
vals = [f(x) for x in xx]
vals = M.array(vals)
mvals = ma.masked_inside(xx, 1.999, 2.001)
subplot(121)
plot(xx, mvals, linewidth=1, c='red')
xlim(-4,4)
ylim(-4,4)
gca().set_aspect('equal', adjustable='box')
show()
How is masked_inside
meant to be used correctly?