问题描述
我正在使用seaborn软件包在Python中绘制热图.我正在绘制的值是离散的,它们是整数-1
,0
和1
.
I am plotting a heatmap in Python with the seaborn package. The values I am plotting are discrete, they are the integers -1
, 0
, and 1
.
我希望热图中的值-1
的单元格显示为绿色,0
为黄色,1
为红色的单元格.
I would like the cells in the heatmap with the value -1
to show up green, those with 0
as yellow, and 1
as red.
是否可以在cubehelix_palette()
或colour_palette()
函数中指定此规则?
Is it possible to specify this ruling in the cubehelix_palette()
or colour_palette()
functions?
推荐答案
您可以使用matplotlib的 ListedColormap 如下:
You can use matplotlib's ListedColormap as follows:
import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap
data = np.random.randint(-1, 2, (10,10)) # Random [-1, 0, 1] data
sns.heatmap(data, cmap=ListedColormap(['green', 'yellow', 'red']), annot=True)
产生:
您可以将字符串'green', 'yellow', 'red'
替换为十六进制颜色(例如'#FF0000'
(等同于'red'
))或rgb颜色(例如(1.,0.,0.)
(也等同于'red'
)).
You can replace the strings 'green', 'yellow', 'red'
with hexcolors such as '#FF0000'
(equivalent to 'red'
) or rgb colors such as (1.,0.,0.)
(also equivalent to 'red'
).
这篇关于将值映射到Seaborn热图中的特定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!