Say I have data in t
and y
arrays. I want to identify the array element indices between t=0.5 to t=1.5 and t=3 to t=4 in order to set the corresponding y values to zero (and put this in ynew
array). I am having trouble with getting the indices into the empty array tnew
, as I just get an output of []
.
import numpy as np
import matplotlib.pyplot as plt
t = np.array([0,0.1,0.2,0.4,0.6,0.65,0.7,0.88,0.92,1.09,1.2,1.28,1.5,1.7,1.9,2.1,2.2,2.33,2.6,2.9,2.99,3.1,3.3,3.4,3.7,3.8,4,4.2])
y = t + 0.2*np.random.randn(len(t))
plt.plot(t,y,'o')
plt.show()
# want ranges t=0.5 to t=1.5 and t=3 to t=4 to have corresponding y=0 values:
for index, item in enumerate(t):
tnew = [] # put index values here
if item > 0.5 and item < 1.5:
tnew.append(index)
#print (index, item)
elif item > 3 and item < 4:
tnew.append(index)
#print (index, item)
print (tnew)
Any ideas?
I want to end up plotting ynew
vs tnew
.
You just need to use the boolean indexing of numpy
.
Add this line: y[((t>0.5)&(t<=1.5))|((t>3)&(t<=4))]=0.