I am working on a program which will create contour data out of numpy arrays, and trying to avoid calls to matplotlib.
I have an array of length L which contains NxN arrays of booleans. I want to convert this into an LxNxN array where, for example, the "True"s in the first inner array get replaced by "red", in the second, by "blue" and so forth.
The following code works as expected:
import numpy as np
import pdb
def new_layer(N,p):
return np.random.choice(a=[False,True],size=(N,N),p=[p,1-p])
a = np.array([new_layer(3,0.5),new_layer(3,0.5),new_layer(3,0.5)]).astype('object')
colors = np.array(["red","green","blue"])
for i in range(np.shape(a)[0]):
b = a[i]
b[np.where(b==True)] = colors[i]
a[i] = b
print(a)
But I am wondering if there is a way to accomplish the same using Numpy's built-in tools, e.g., indexing. I am a newcomer to Numpy and I suspect there is a better way to do this but I can't think what it would be. Thank you.