I have three ndarrays as follows, where x and y are my coordinate grids and z is my data:
x: [[-11. -11. -11. -11. -11. ]
[ -9.25 -9.25 -9.25 -9.25 -9.25]
[ -7.5 -7.5 -7.5 -7.5 -7.5 ]
[ -5.75 -5.75 -5.75 -5.75 -5.75]
[ -4. -4. -4. -4. -4. ]]
y: [[51. 52.25 53.5 54.75 56. ]
[51. 52.25 53.5 54.75 56. ]
[51. 52.25 53.5 54.75 56. ]
[51. 52.25 53.5 54.75 56. ]
[51. 52.25 53.5 54.75 56. ]]
z: [[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 21. 0. 0. 0.]
[21. 21. 4. 0. 0.]
[21. 21. 4. 4. 0.]]
I wish to do something like this:
coords = np.stack((x, y)).T
for (x,y), value in np.ndenumerate(z):
xx,yy = coords[x][y]
if not m.is_land(xx,yy):
z[x][y] = 0
How do I do this correctly in numpy? Better still would be to create a new array instead of trying to change z.
UPDATE
if I do print(z) after the above code I get:
z: [[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 4. 0. 0.]
[0. 0. 0. 0. 0.]]
but when I use z later in my code I get:
plt.contour(x[:,0], x[0,:], z.T,linewidths=0.5,colors='k',z=99)
TypeError: 'int' object is not subscriptable
my code seems to be breaking z somehow.
m.is_land? What are you trying to do?m.is_landdoes.