code: def palettize(arr, colors, values):
      
      new_arr = np.digitize(arr.ravel(),
                            np.concatenate((values,
                                            [max(np.nanmax(arr),
                                                 values.max()) + 1])))
      new_arr -= 1
      new_arr = new_arr.clip(min=0, max=len(values) - 1)
      try:
          new_arr = np.ma.array(new_arr.reshape(arr.shape), mask=arr.mask)
      except AttributeError:
          new_arr = new_arr.reshape(arr.shape)
  
      return new_arr, tuple(colors)
docs: From start *values* apply *colors* to *data*.