如何自定义热图的颜色栏

时间:2019-10-28 19:27:00

标签: python seaborn heatmap colorbar

我绘制了下面的热图。我想使用四种颜色来可视化与每个单元格关联的值。

我已经在heatmap()函数中定义了vmin和vmax参数。当在vmin和vmax之间映射值时,似乎颜色均匀分布。但是,应用颜色的时间间隔的长度因颜色而异。

代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns

X=np.array([
    [ 81.31,  99.91,  99.91,  81.31,  99.91],
    [ 99.91,  99.91,  99.91,  99.91,  99.91],
    [ 99.87,  99.87,  99.87,  99.87,  99.87],
    [ 99.98,  99.98,  99.98,  99.98,  99.98],
    [ 75.64,  75.64,  75.64,  75.62,  75.64],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 98.01,  98.01,  98.01,  97.98,  98.01],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 93.75,  99.98,  99.98,  93.75,  99.98],
    [ 93.64,  93.64,  93.64,  73.64,  73.64],
    [ 99.98,  79.98,  79.98,  99.98,  99.98],
    [ 99.91,  99.91,  99.91,  99.91,  99.91],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 99.96,  99.96,  69.96,  69.96,  99.96],
    [ 99.98,  99.98,  49.98,  49.98,  99.98],
    [ 99.98,  99.98,  99.98,  99.98,  99.98],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 99.89,  99.89,  99.89,  99.89,  99.89],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 99.87,  99.87,  99.87,  99.87,  99.87]
])
index=['Test{:02d}'.format(i) for i in range(22)]
cols=['Day{}'.format(i) for i in range(1,6)]
X=pd.DataFrame(X,index=index,columns=cols)

fig, ax= plt.subplots(figsize=(12,12))


# colors: red=[0,50], yellow=[50,75], orange=[75,90], green=[90,100]
sns.heatmap(
    data=X,
    ax=ax,
    vmax=100, vmin=0,
    cmap=sns.color_palette(['red', 'yellow', 'orange', 'green']), 
    linewidths=.5, linecolor='lightgray',
    annot=True, fmt=".1f",
    cbar_kws={'pad': .02, 'ticks': [0, 50, 75, 90, 100]},
)
ax.set_yticklabels(ax.get_yticklabels(), rotation=0);

我希望所有单元格的值都在

之间
  • 100和90涂成绿色;
  • 90和75涂成橙色;
  • [li] 75和50染成黄色;
  • 50和0被涂成红色。

更新时间:2019-11-02

如果其他人可能遇到类似的问题,这是我使用@ImportanceOfBeingErnest的指示提出的解决方案:

导入库

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import seaborn as sns

创建自定义地图

# define 100 colors using an existing color map
newcolors = plt.get_cmap('viridis',100).colors

# assign new colors of interest
newcolors[  : 50, :] = colors.to_rgba('red')
newcolors[50: 75, :] = colors.to_rgba('yellow')
newcolors[75: 90, :] = colors.to_rgba('orange')
newcolors[90:100, :] = colors.to_rgba('green')

# create the customized color map
mycmap = colors.ListedColormap(newcolors)

生成样本数据

X = np.array([
    [ 81.31,  99.91,  99.91,  81.31,  99.91],
    [ 99.91,  99.91,  99.91,  99.91,  99.91],
    [ 99.87,  99.87,  99.87,  99.87,  99.87],
    [ 99.98,  99.98,  99.98,  99.98,  99.98],
    [ 75.64,  75.64,  75.64,  75.62,  75.64],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 98.01,  98.01,  98.01,  97.98,  98.01],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 93.75,  99.98,  99.98,  93.75,  99.98],
    [ 93.64,  93.64,  93.64,  73.64,  73.64],
    [ 99.98,  79.98,  79.98,  99.98,  99.98],
    [ 99.91,  99.91,  99.91,  99.91,  99.91],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 99.96,  99.96,  69.96,  69.96,  99.96],
    [ 99.98,  99.98,  49.98,  49.98,  99.98],
    [ 99.98,  99.98,  99.98,  99.98,  99.98],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 99.89,  99.89,  99.89,  99.89,  99.89],
    [100.  , 100.  , 100.  , 100.  , 100.  ],
    [ 99.87,  99.87,  99.87,  99.87,  99.87]
])
index = ['Test{:02d}'.format(i) for i in range(22)]
cols = ['Day{}'.format(i) for i in range(1,6)]
X = pd.DataFrame(X,index=index,columns=cols)

制作情节

# use the new color map
fig, ax = plt.subplots(figsize=(12,12))
sns.heatmap(
    data=X,
    ax=ax,
    vmax=100, vmin=0,
    cmap=mycmap,
    linewidths=.5, 
    linecolor='lightgray',
    annot=True, 
    fmt=".1f",
    cbar_kws={
        'pad': .02, 
        'ticks': [0, 50, 75, 90, 100],
    },
)
ax.set_yticklabels(ax.get_yticklabels(), rotation=0);

结果

heatmap

0 个答案:

没有答案