inserting nan at a point in two arrays

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

inserting nan at a point in two arrays

David Pine
I want to plot tan(x) vs x with a gap in the line plot when x goes thought pi/2, the point where tan(x) goes from +infinity to -infinity.  The idea is to insert nan between the points in the tan(x) and x arrays at x=pi/2, which will leave the desired gap when plot is called.  Does anyone have an efficient way to do this?
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: inserting nan at a point in two arrays

PHobson
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of David Pine
> Sent: Tuesday, July 20, 2010 5:08 AM
> To: [hidden email]
> Subject: [SciPy-User] inserting nan at a point in two arrays
>
> I want to plot tan(x) vs x with a gap in the line plot when x goes
> thought pi/2, the point where tan(x) goes from +infinity to -infinity.
> The idea is to insert nan between the points in the tan(x) and x arrays
> at x=pi/2, which will leave the desired gap when plot is called.  Does
> anyone have an efficient way to do this?

Simplest thing to do would be to set a threshold (10,000?) and use a masked array.

# code ---
import numpy as np
import matplotlib.pyplot as plt

threshold = 1e5
x = np.arange(0, 2*np.pi, np.pi/16)
y = np.tan(x)
ym = np.ma.MaskedArray(y, np.abs(y) > threshold)

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.plot(x, ym, 'k-')
ax1.set_xlabel('Radians')
ax1.set_ylabel('Masked Tangent Function')
# ---/code

Hope that help
-paul


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: inserting nan at a point in two arrays

David Pine
Paul,

Marvelous!  Thanks.  That does the trick.

Dave

On Jul 21, 2010, at 12:10 AM, <[hidden email]> wrote:

>> -----Original Message-----
>> From: [hidden email] [mailto:[hidden email]]
>> On Behalf Of David Pine
>> Sent: Tuesday, July 20, 2010 5:08 AM
>> To: [hidden email]
>> Subject: [SciPy-User] inserting nan at a point in two arrays
>>
>> I want to plot tan(x) vs x with a gap in the line plot when x goes
>> thought pi/2, the point where tan(x) goes from +infinity to -infinity.
>> The idea is to insert nan between the points in the tan(x) and x arrays
>> at x=pi/2, which will leave the desired gap when plot is called.  Does
>> anyone have an efficient way to do this?
>
> Simplest thing to do would be to set a threshold (10,000?) and use a masked array.
>
> # code ---
> import numpy as np
> import matplotlib.pyplot as plt
>
> threshold = 1e5
> x = np.arange(0, 2*np.pi, np.pi/16)
> y = np.tan(x)
> ym = np.ma.MaskedArray(y, np.abs(y) > threshold)
>
> fig = plt.figure()
> ax1 = fig.add_subplot(1,1,1)
> ax1.plot(x, ym, 'k-')
> ax1.set_xlabel('Radians')
> ax1.set_ylabel('Masked Tangent Function')
> # ---/code
>
> Hope that help
> -paul
>
>
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user