Seaborn热图中的自定义调色板间隔 [英] Custom color palette intervals in seaborn heatmap

查看:213
本文介绍了Seaborn热图中的自定义调色板间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用seaborn库绘制



现在,我正在苦苦挣扎的问题是那可怕的热图(下面的视图)将色标平均分配,因此大多数数据具有相同的颜色(因为数据分布不均匀)。



我找不到



假设我有以下十六进制颜色值数组:



['#e5e5ff','#acacdf','#7272bf','#39399f','#000080']



有没有一种设置颜色的方法,例如



[(threshold_0,hex_0),( threshold_1,hex_1),...,(threshold_n,hex_n)]



其中 threshold_i 是[0,1)范围内的值。





感谢任何帮助。



PS:用于说明的当前热图:



解决方案

我能够找出答案(没有我认为这非常干净)解决方案,它使用 matplotlib.colors.LinearSegmentedColormap



代码如下所示:

 #注意:jupyter笔记本模式
%matplotlib内联

从matplotlib.colors导入seaborn作为sns
从color导入LinearSegmentedColormap

boundary = [0.0,0.05,0.1,0.25,0.5,0.75,0.9,1.0]#自定义边界

#这里我生成的颜色是
#的两倍,因此我可以更清晰地修剪边界
hex_colors = sns.light_palette('navy',n_colors = len(boundaries)* 2 + 2 ,as_cmap = False).as_hex()
hex_colors = [hex_colors [i] for i in range(0,len(hex_colors),2)]

colors = list(zip(boundaries ,hex_colors))

custom_color_map = LinearSegmentedColormap.from_list(
name ='custom_navy',
colors = colors,


sns .heatmap(
vmin = 0.0,
vmax = 1.0,
data = data,
cma p = custom_color_map,
xticklabels =标签,
yticklabels =标签,
线宽= 0.75,


I am trying to plot a heatmap using seaborn library.

The plotting function looks like this:

def plot_confusion_matrix(data, labels, **kwargs):
    """Visualize confusion matrix as a heat map."""
    col_map = kwargs.get('color_palette', sns.light_palette('navy', n_colors=5, as_cmap=False))

    sns.heatmap(
        vmin=0.0,
        vmax=1.0,
        data=data,
        cmap=col_map,
        xticklabels=labels,
        yticklabels=labels,
        linewidths=0.75,
    )

The histogram of the data, however, looks like this:

Now the issue I am struggling with is that seaborn heatmap(view bellow) splits evenly the color scale and hence most of the data has the same color (since the data is not evenly distributed).

I was not able to find out how to set some sort of intervals or boundaries for the color levels.

Suppose I have the following array of hex color values:

['#e5e5ff', '#acacdf', '#7272bf', '#39399f', '#000080']

Is there a way to set up a color such as

[(threshold_0, hex_0), (threshold_1, hex_1), ..., (threshold_n, hex_n)]

where threshold_i is a value in range [0, 1)


Appreciate any help.

PS: current heatmap for illustration:

解决方案

I was able to find out (not very clean tho, in my opinion) solution to this, which is using matplotlib.colors.LinearSegmentedColormap.

The code looks like this:

# NOTE: jupyter notebook mode
%matplotlib inline

import seaborn as sns
from matplotlib.colors import LinearSegmentedColormap

boundaries = [0.0, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0]  # custom boundaries

# here I generated twice as many colors, 
# so that I could prune the boundaries more clearly
hex_colors = sns.light_palette('navy', n_colors=len(boundaries) * 2 + 2, as_cmap=False).as_hex()
hex_colors = [hex_colors[i] for i in range(0, len(hex_colors), 2)]

colors=list(zip(boundaries, hex_colors))

custom_color_map = LinearSegmentedColormap.from_list(
    name='custom_navy',
    colors=colors,
)

 sns.heatmap(
        vmin=0.0,
        vmax=1.0,
        data=data,
        cmap=custom_color_map,
        xticklabels=labels,
        yticklabels=labels,
        linewidths=0.75,
  )

这篇关于Seaborn热图中的自定义调色板间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆