function domain problems

Hi all,

I'm having trouble plotting functions were the domain aren't the real
numbers (R). Functions like x**-n, log(x), etc. Does anyone know if
there's a simple way of solving this problem. Here's a little script and
it's error:

from pylab import *

def f(x):
        return log(x)

x = arange(-3, 4)
plot(x, f(x))
show()

Output:

Traceback (most recent call last):
  File "test", line 7, in ?
    plot(x, f(x))
  File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line
2019, in plot
    ret = gca().plot(*args, **kwargs)
  File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 2111,
in plot
    self.autoscale_view(scalex=scalex, scaley=scaley)
  File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 975,
in autoscale_view
    if scaley: self.set_ylim(locator.autoscale())
  File "/usr/lib/python2.4/site-packages/matplotlib/ticker.py", line
839, in autoscale
    return take(self.bin_boundaries(dmin, dmax), [0,-1])
  File "/usr/lib/python2.4/site-packages/matplotlib/ticker.py", line
809, in bin_boundaries
    scale, offset = scale_range(vmin, vmax, nbins)
  File "/usr/lib/python2.4/site-packages/matplotlib/ticker.py", line
783, in scale_range
    ex = divmod(math.log10(-meanv), 1)[0]
OverflowError: math range error

Thanks!

I suggest using masked arrays to ensure you are only trying to plot real numbers:

def f(x):
     y = log(x)
     ygood = (y < 1e38) & (y > -1e-38)
     yy = nx.ma.masked_where(ygood == 0, y)
     return yy

Note the parentheses and peculiar use of bitwise-and in the definition of ygood. This is a hack to get around the fact that the logical-and operator in python cannot be overloaded at present, and so is not available in numpy.

Eric

Davidlohr Bueso A. wrote:

···

Hi all,

I'm having trouble plotting functions were the domain aren't the real
numbers (R). Functions like x**-n, log(x), etc. Does anyone know if
there's a simple way of solving this problem. Here's a little script and
it's error:

from pylab import *

def f(x):
        return log(x)

x = arange(-3, 4)
plot(x, f(x))
show()