I want to map each numpy array to a color to create an image.
For example: if I have the numpy array:
[ [0 0 1 3]
[0 2 4 5]
[1 2 3 6] ]
I want to make an image by mapping all values below 3 to blue like
[ [blue Blue blue No-Color]
[blue blue No-color No-color]
[blue blue No-Color No-color]
CodePudding user response:
You can make a two-color color map. Then make an array with 1 or 0 depending on your condition and pass both to pyplot.imshow()
:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
# white and blue
color = ListedColormap([(1,1,1), (0,0,1)])
a = np.array([
[0, 0, 1, 3],
[0, 2, 4, 5],
[1, 2, 3, 6]
])
plt.axis('off')
plt.imshow(a < 3, cmap=color)
CodePudding user response:
IIUC, You need to create (0,0,1)
for blue and (1,1,1)
for white then you can do this like below:
import numpy as np
import matplotlib.pyplot as plt
arr = np.array([
[0, 0, 1, 3],
[0, 2, 4, 5],
[1, 2, 3, 6]
])
chk = np.where(arr<3, 0, 1)
img_from_arr = np.dstack((chk, chk, np.ones((3,4))))
plt.imshow(img_from_arr)
Output: