quiver and Inf values

A student of mine recently noticed that sometimes, quiver plots were coming up empty (using the plot_vector_field function from Sage, which passes everything on to quiver). Upon investigation, we saw that some of the array entries passed in were infinity because of where we happened to evaluate the function. It was relatively easy to correct in our case (change the evaluation to miss the bad point), but is there a better way to handle this? Can this be considered a bug in quiver (i.e., returning a blank plot when one of the vectors has an infinite coordinate?).

Here is some example code illustrating the problem:

import pylab
import numpy
step=1
X,Y = numpy.meshgrid( numpy.arange(-1,1.1,step),numpy.arange(-1,1.1,step) )
U = 1/X
V = Y
pylab.figure()
Q = pylab.quiver( X,Y,U, V)
pylab.savefig("test.png")

When you change step to something that avoids an evaluation at x=0 (say, step=0.13), you get a nice plot.

Is this something that we should be preprocessing in Sage before calling quiver, masking those "bad" points or something? I haven't used masking before, but I'd like to fix Sage's plot_vector_field function to return something sensible, even when the function happens to be infinite at one of the points.

Thanks,

Jason

···

--
Jason Grout

I’m not sure why quiver does not plot any arrows in that case, but it’s also easy enough to mask out the values yourself:

U = 1/X
U = numpy.ma.array(U, mask=numpy.isinf(U))
V = Y

V = numpy.ma.array(V, mask=numpy.isinf(V))

You can also catch NaN values by using ~numpy.isfinite() instead of numpy.isinf().

Ryan

···

On Fri, Feb 13, 2009 at 12:08 PM, <jason-sage@…2130…> wrote:

A student of mine recently noticed that sometimes, quiver plots were

coming up empty (using the plot_vector_field function from Sage, which

passes everything on to quiver). Upon investigation, we saw that some

of the array entries passed in were infinity because of where we

happened to evaluate the function. It was relatively easy to correct in

our case (change the evaluation to miss the bad point), but is there a

better way to handle this? Can this be considered a bug in quiver (i.e.,

returning a blank plot when one of the vectors has an infinite coordinate?).

Here is some example code illustrating the problem:

import pylab

import numpy

step=1

X,Y = numpy.meshgrid( numpy.arange(-1,1.1,step),numpy.arange(-1,1.1,step) )

U = 1/X

V = Y

pylab.figure()

Q = pylab.quiver( X,Y,U, V)

pylab.savefig(“test.png”)

When you change step to something that avoids an evaluation at x=0 (say,

step=0.13), you get a nice plot.

Is this something that we should be preprocessing in Sage before calling

quiver, masking those “bad” points or something? I haven’t used masking

before, but I’d like to fix Sage’s plot_vector_field function to return

something sensible, even when the function happens to be infinite at one

of the points.


Ryan May
Graduate Research Assistant
School of Meteorology

University of Oklahoma

Ryan May wrote:

    A student of mine recently noticed that sometimes, quiver plots were
    coming up empty (using the plot_vector_field function from Sage, which
    passes everything on to quiver). Upon investigation, we saw that some
    of the array entries passed in were infinity because of where we
    happened to evaluate the function. It was relatively easy to correct in
    our case (change the evaluation to miss the bad point), but is there a
    better way to handle this? Can this be considered a bug in quiver (i.e.,
    returning a blank plot when one of the vectors has an infinite
    coordinate?).

    Here is some example code illustrating the problem:

    import pylab
    import numpy
    step=1
    X,Y = numpy.meshgrid(
    numpy.arange(-1,1.1,step),numpy.arange(-1,1.1,step) )
    U = 1/X
    V = Y
    pylab.figure()
    Q = pylab.quiver( X,Y,U, V)
    pylab.savefig("test.png")

    When you change step to something that avoids an evaluation at x=0 (say,
    step=0.13), you get a nice plot.

    Is this something that we should be preprocessing in Sage before calling
    quiver, masking those "bad" points or something? I haven't used masking
    before, but I'd like to fix Sage's plot_vector_field function to return
    something sensible, even when the function happens to be infinite at one
    of the points.

I'm not sure why quiver does not plot any arrows in that case, but it's also easy enough to mask out the values yourself:

U = 1/X
U = numpy.ma.array(U, mask=numpy.isinf(U))
V = Y
V = numpy.ma.array(V, mask=numpy.isinf(V))

You can also catch NaN values by using ~numpy.isfinite() instead of numpy.isinf().

This is a good use case for numpy.ma.masked_invalid:

In [2]:numpy.ma.masked_invalid?
Type: function
Base Class: <type 'function'>
String Form: <function masked_invalid at 0xb62bccdc>
Namespace: Interactive
File: /usr/local/lib/python2.5/site-packages/numpy/ma/core.py
Definition: numpy.ma.masked_invalid(a, copy=True)
Docstring:
     Mask the array for invalid values (NaNs or infs).
     Any preexisting mask is conserved.

Eric

···

On Fri, Feb 13, 2009 at 12:08 PM, <jason-sage@...2130... > <mailto:jason-sage@…2130…>> wrote:

Ryan

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

------------------------------------------------------------------------

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H

------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Eric Firing wrote:

Ryan May wrote:

    A student of mine recently noticed that sometimes, quiver plots were
    coming up empty (using the plot_vector_field function from Sage, which
    passes everything on to quiver). Upon investigation, we saw that some
    of the array entries passed in were infinity because of where we
    happened to evaluate the function. It was relatively easy to correct in
    our case (change the evaluation to miss the bad point), but is there a
    better way to handle this? Can this be considered a bug in quiver (i.e.,
    returning a blank plot when one of the vectors has an infinite
    coordinate?).

    Here is some example code illustrating the problem:

    import pylab
    import numpy
    step=1
    X,Y = numpy.meshgrid(
    numpy.arange(-1,1.1,step),numpy.arange(-1,1.1,step) )
    U = 1/X
    V = Y
    pylab.figure()
    Q = pylab.quiver( X,Y,U, V)
    pylab.savefig("test.png")

    When you change step to something that avoids an evaluation at x=0 (say,
    step=0.13), you get a nice plot.

    Is this something that we should be preprocessing in Sage before calling
    quiver, masking those "bad" points or something? I haven't used masking
    before, but I'd like to fix Sage's plot_vector_field function to return
    something sensible, even when the function happens to be infinite at one
    of the points.

I'm not sure why quiver does not plot any arrows in that case, but it's also easy enough to mask out the values yourself:

U = 1/X
U = numpy.ma.array(U, mask=numpy.isinf(U))
V = Y
V = numpy.ma.array(V, mask=numpy.isinf(V))

You can also catch NaN values by using ~numpy.isfinite() instead of numpy.isinf().

This is a good use case for numpy.ma.masked_invalid:

In [2]:numpy.ma.masked_invalid?
Type: function
Base Class: <type 'function'>
String Form: <function masked_invalid at 0xb62bccdc>
Namespace: Interactive
File: /usr/local/lib/python2.5/site-packages/numpy/ma/core.py
Definition: numpy.ma.masked_invalid(a, copy=True)
Docstring:
    Mask the array for invalid values (NaNs or infs).
    Any preexisting mask is conserved.

Thanks for both of your replies. So I tried the following:

import pylab
import numpy
step=1
X,Y = numpy.meshgrid( numpy.arange(-1,1.1,step),numpy.arange(-1,1.1,step) )
U = numpy.ma.masked_invalid(1/X)
V = numpy.ma.masked_invalid(Y)
pylab.figure()
Q = pylab.quiver( X,Y,U, V)
pylab.savefig("test.png")

and I still didn't get a plot. I noticed two things:

1. The unmasked portion of each array might be different; I hope quiver can handle that.

2. Even when I called pylab.quiver(X,Y,U,U) (so that the masks lined up), I still didn't get a plot.

Does quiver handle masks properly, or did I just do something wrong?

Jason

···

On Fri, Feb 13, 2009 at 12:08 PM, <jason-sage@...2130... >> <mailto:jason-sage@…2130…>> wrote:

It should, but there was a release with a bug in the masked support for quiver. What version are you running?

import matplotlib; print matplotlib.version

Ryan

···

On Fri, Feb 13, 2009 at 2:30 PM, <jason-sage@…2130…> wrote:

Eric Firing wrote:

Ryan May wrote:

On Fri, Feb 13, 2009 at 12:08 PM, <jason-sage@…2130… mailto:jason-sage@...2130...> wrote:

A student of mine recently noticed that sometimes, quiver plots were

coming up empty (using the plot_vector_field function from Sage, which

passes everything on to quiver).  Upon investigation, we saw that some

of the array entries passed in were infinity because of where we

happened to evaluate the function.  It was relatively easy to correct in

our case (change the evaluation to miss the bad point), but is there a

better way to handle this? Can this be considered a bug in quiver (i.e.,

returning a blank plot when one of the vectors has an infinite

coordinate?).



Here is some example code illustrating the problem:





import pylab

import numpy

step=1

X,Y = numpy.meshgrid(

numpy.arange(-1,1.1,step),numpy.arange(-1,1.1,step) )

U = 1/X

V = Y

pylab.figure()

Q = pylab.quiver( X,Y,U, V)

pylab.savefig("test.png")



When you change step to something that avoids an evaluation at x=0 (say,

step=0.13), you get a nice plot.



Is this something that we should be preprocessing in Sage before calling

quiver, masking those "bad" points or something?  I haven't used masking

before, but I'd like to fix Sage's plot_vector_field function to return

something sensible, even when the function happens to be infinite at one

of the points.

I’m not sure why quiver does not plot any arrows in that case, but it’s also easy enough to mask out the values yourself:

U = 1/X

U = numpy.ma.array(U, mask=numpy.isinf(U))

V = Y

V = numpy.ma.array(V, mask=numpy.isinf(V))

You can also catch NaN values by using ~numpy.isfinite() instead of numpy.isinf().

This is a good use case for numpy.ma.masked_invalid:

In [2]:numpy.ma.masked_invalid?

Type: function

Base Class: <type ‘function’>

String Form: <function masked_invalid at 0xb62bccdc>

Namespace: Interactive

File: /usr/local/lib/python2.5/site-packages/numpy/ma/core.py

Definition: numpy.ma.masked_invalid(a, copy=True)

Docstring:

Mask the array for invalid values (NaNs or infs).

Any preexisting mask is conserved.

Thanks for both of your replies. So I tried the following:

import pylab

import numpy

step=1

X,Y = numpy.meshgrid( numpy.arange(-1,1.1,step),numpy.arange(-1,1.1,step) )

U = numpy.ma.masked_invalid(1/X)

V = numpy.ma.masked_invalid(Y)

pylab.figure()

Q = pylab.quiver( X,Y,U, V)

pylab.savefig(“test.png”)

and I still didn’t get a plot. I noticed two things:

  1. The unmasked portion of each array might be different; I hope quiver can handle that.

  2. Even when I called pylab.quiver(X,Y,U,U) (so that the masks lined up), I still didn’t get a plot.

Does quiver handle masks properly, or did I just do something wrong?


Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
Sent from: Norman Oklahoma United States.