6

I would like to pass a colormap to a matplotlib.imshow to plot a NxN array where each cell can only take the values -1,0 and 1. I would like my colormap to map exactly one color (in hex code) to each value.

I just have no idea how to define a custom colormap which is not a gradation between two colors.

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71

2 Answers2

5

A solution to a very similar question was provided here. In summary ,

import matplotlib
matplotlib.colors.ListedColormap(list-of-colours)

is your friend.

fernando
  • 176
  • 3
3

Found the answer to my question, as fernando suggested:

import matplotlib
colorsList = [(tuple rgb color 1),(tuple rgb color 2),(tuple rgb color 3)]
CustomCmap = matplotlib.colors.ListedColormap(colorsList)
xnx
  • 24,509
  • 11
  • 70
  • 109
Learning is a mess
  • 7,479
  • 7
  • 35
  • 71