Ryan May wrote:
> On Fri, Feb 13, 2009 at 12:08 PM, <[email protected]
> <mailto:[email protected]>> 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
>
> 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
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
------------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users