Seaborn热图中的自定义调色板间隔

编程入门 行业动态 更新时间:2024-10-22 21:29:22
本文介绍了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热图中的自定义调色板间隔

本文发布于:2023-11-08 20:40:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:调色板   自定义   间隔   图中   Seaborn

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!