Matplotlib:具有三种颜色的自定义颜色图

Lea*_*ess 5 python matplotlib

我想将颜色matplotlib.imshow图传递给 a以绘制一个 NxN 数组,其中每个单元格只能取值 -1,0 和 1。我希望我的颜色图将一种颜色(以十六进制代码)映射到每个值。

我只是不知道如何定义一个自定义颜色图,它不是两种颜色之间的渐变。

小智 5

这里提供了一个非常类似问题的解决方案。总之 ,

import matplotlib
matplotlib.colors.ListedColormap(list-of-colours)
Run Code Online (Sandbox Code Playgroud)

是你的朋友。