# Problems with auto scaling with infinite values

Hi all,

If I try to plot a graph with some y-values as -inf (which is very
common in physical sciences as we often take log10 of amplitude values
which can be zero), then matplotlib raises an exception:

/usr/lib/python2.5/site-packages/matplotlib/ticker.py in
scale_range(vmin, vmax, n, threshold)
814 offset = 10**ex
815 else:
--> 816 ex = divmod(math.log10(-meanv), 1)[0]
817 offset = -10**ex
818 ex = divmod(math.log10(dv/n), 1)[0]

<type 'exceptions.OverflowError'>: math range error

It is clear where the problem is, matplotlib takes log10 of the mean
y-range (in this case infinity) to auto scale the y-axis.

I know I can modify my input values to get around this problem, but it
is so common to want to plot data (some of which may be zero) in say a
decibel scale, that having a workaround for the case of -inf input
would be nice. I tried to look into writing one myself, but got
quickly lost. If someone could point me at the files/functions I
should look at I will have a go.

Hope somebody can help!

Best regards,
John Travers

I think this might trace back to Interval.get_bounds() in src/_transforms.h.
Maybe we could consider an additional Interval method like
Interval.get_finite_bounds(), which could do something like numpy's isfinite
function to filter values that can not be plotted. But I'm not an expert on
mpl's transforms, so perhaps someone more knowledgeable can comment.

Darren

···

On Friday 13 July 2007 09:13:03 am John Travers wrote:

Hi all,

If I try to plot a graph with some y-values as -inf (which is very
common in physical sciences as we often take log10 of amplitude values
which can be zero), then matplotlib raises an exception:

/usr/lib/python2.5/site-packages/matplotlib/ticker.py in
scale_range(vmin, vmax, n, threshold)
814 offset = 10**ex
815 else:
--> 816 ex = divmod(math.log10(-meanv), 1)[0]
817 offset = -10**ex
818 ex = divmod(math.log10(dv/n), 1)[0]

<type 'exceptions.OverflowError'>: math range error

It is clear where the problem is, matplotlib takes log10 of the mean
y-range (in this case infinity) to auto scale the y-axis.

I know I can modify my input values to get around this problem, but it
is so common to want to plot data (some of which may be zero) in say a
decibel scale, that having a workaround for the case of -inf input
would be nice. I tried to look into writing one myself, but got
quickly lost. If someone could point me at the files/functions I
should look at I will have a go.

I'm happy to do this, if someone can advise how to get a portable
isinf. I tried including math.h after reading

but the symbol isn't found there on my system. I would be happy to
import it from numpy (since we can now rely on it, woohoo) but am
having trouble here too.... I see isfinite defined in
umathmodule.c.src (and also in ufuncobject.h but only for _MSC_VER.
If anyone can advise on how to get isinf or isfinite, pease do.

JDH

···

On 7/13/07, Darren Dale <dd55@...163...> wrote:

I think this might trace back to Interval.get_bounds() in src/_transforms.h.
Maybe we could consider an additional Interval method like
Interval.get_finite_bounds(), which could do something like numpy's isfinite
function to filter values that can not be plotted. But I'm not an expert on
mpl's transforms, so perhaps someone more knowledgeable can comment.

> I think this might trace back to Interval.get_bounds() in
> src/_transforms.h. Maybe we could consider an additional Interval method
> like
> Interval.get_finite_bounds(), which could do something like numpy's
> isfinite function to filter values that can not be plotted. But I'm not
> an expert on mpl's transforms, so perhaps someone more knowledgeable can
> comment.

I'm happy to do this, if someone can advise how to get a portable
isinf.

[...]

I would be happy to
import it from numpy (since we can now rely on it, woohoo) but am
having trouble here too....

If we can figure out how to get it from numpy, we can use numpy's isnan as
well, and drop that bit of extension code from mpl's sources.

···

On Friday 13 July 2007 10:32:15 am John Hunter wrote:

On 7/13/07, Darren Dale <dd55@...163...> wrote:

I see isfinite defined in
umathmodule.c.src (and also in ufuncobject.h but only for _MSC_VER.
If anyone can advise on how to get isinf or isfinite, pease do.

Darren Dale wrote:

If we can figure out how to get it from numpy, we can use numpy's isnan as well, and drop that bit of extension code from mpl's sources.

Done in r3512. Hurray for inclusion instead of code duplication. (I originally copied that stuff from numarray, which inspired numpy's design of that stuff, but since back then would couldn't depend on either...)

···

On Friday 13 July 2007 10:32:15 am John Hunter wrote:

>> I'm happy to do this, if someone can advise how to get a portable
>> isinf.

Also in r3512, I inserted the definition of isfinite, copied from numpy's umathmodule.c.src at the top of _transforms.cpp. If it works for numpy, it should work for us.

I'm slightly worried that this stuff will break on MSVC... Does anyone have the ability to test this svn revision sooner than later?

-Andrew