You can use matplotlib’s
ListedColormap
as follows:
import numpy as npimport seaborn as snsfrom matplotlib.colors import ListedColormapdata = np.random.randint(-1, 2, (10,10)) # Random [-1, 0, 1] datasns.heatmap(data, cmap=ListedColormap(['green', 'yellow', 'red']), annot=True)
which yields:
您可以将字符串“green”、“yellow”、“red”替换为诸如
“#FF0000”(相当于“red”
)或rgb颜色,如(1,0,0.)`(也可以是相当于“红色”。



