2

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 sky-blue green]
[blue sky-blue green green] 
[blue sky-blue green green]
1

1 Answer 1

2

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)

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Can i make 3-4 colors with this?
Yes. You an make a colormap with more colors, or use one of the built-in ones. Then you need an array with more values describing where to use the colors.
Can you give an example for 4 color map?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.