5
votes

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.

2

2 Answers

4
votes

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

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

is your friend.

3
votes

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)