[SciPy-User] inserting nan at a point in two arrays

PHobson@Geosynte... PHobson@Geosynte...
Tue Jul 20 11:10:58 CDT 2010


> -----Original Message-----
> From: scipy-user-bounces@scipy.org [mailto:scipy-user-bounces@scipy.org]
> On Behalf Of David Pine
> Sent: Tuesday, July 20, 2010 5:08 AM
> To: scipy-user@scipy.org
> 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




More information about the SciPy-User mailing list