0

I have the following rgb color schema and a data array with the shape (500, 500), after each color there is the range of values in which a specific color should be used, so if one item of the array is lower than 5.0, I will use the 255 255 255 color, if another item is between 5.0 and 7.5 then 0 191 255 will be used. So how could I do this?

# r g b
255 255 255 # <5.0
0 191 255   # 5.0 - 7.5
0 178 238   # 7.5 - 10.0    
0 154 205   # 10.0 - 12.5   
0 104 139   # 12.5 - 15.0   
0 255 127   # 15.0 - 17.5   
0 210 102   # 17.5 - 20.0   
0 185  85   # 20.0 - 22.5   
0 139    69 # 22.5 - 25.0   
255 255   0 # 25.0 - 27.5   
205 205   0 # 27.5 - 30.0   
215 180   0 # 30.0 - 32.5   
200 135   0 # 32.5 - 35.0   
225  60   0 # 35.0 - 37.5
150   0   0 # 37.5 - 40.0   
191  62 255 # 40.0 - 42.5   
178  58 238 # 42.5 - 45.0   
154  50 205 # 45.0 - 47.5   
104  34 139 # 47.5 - 50.0   
255 255 255 # > 50.0
i'mlaguiar
  • 21
  • 2
  • 7

1 Answers1

1

For discrete colors it makes sense to use a ListedColormap and a BoundaryNorm:

import numpy as np
import matplotlib.colors

rgb = [[255, 255, 255], # <5.0
       [0, 191, 255],   # 5.0 - 7.5
       [0, 178, 238]]   # 7.5 - 10.0 
rgb=np.array(rgb)/255.

val = np.array([0,5,7.5,10])

cmap = matplotlib.colors.ListedColormap(rgb,"")
norm = matplotlib.colors.BoundaryNorm(val,len(val)-1)

#test:
print(np.array(cmap(norm(6)))*255)
# prints [   0.  191.  255.  255.] as expected
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712