Just use a ListedColormap
.
As a quick (but ugly) example:
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
cmap = ListedColormap(['red', 'green', 'blue', 'black'], 'indexed')
fig, ax = plt.subplots()
im = ax.imshow([range(4)], interpolation='none', cmap=cmap)
fig.colorbar(im)
plt.show()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…