简体   繁体   English

Seaborn 自定义范围热图

[英]Seaborn custom range heatmap

I need to build custom seaborn heatmap-like plot according to these requirements:我需要根据这些要求构建自定义 seaborn 类似 plot 的热图:

import pandas as pd
df = pd.DataFrame({"A": [0.3, 0.8, 1.3], 
                   "B": [4, 9, 15], 
                   "C": [650, 780, 900]})

df_info = pd.DataFrame({"id": ["min", "max"],
                   "A": [0.5, 0.9], 
                   "B": [6, 10], 
                   "C": [850, 880]})
df_info = df_info.set_index('id')

df df


    A      B    C
0   0.3    4    650
1   0.8    9    780
2   1.3    15   900

df_info df_info

id      A      B    C
            
min     0.5    6    850
max     0.9    10   880

Each value within df is supposed to be within a range defined in df_info . df中的每个值都应该在df_info中定义的范围内。
For example the values for the column A are considered normal if they are within 0.5 and 0.9.例如,如果A列的值在 0.5 和 0.9 之间,则认为它们是正常的。 Values that are outside the range should be colorized using a custom heatmap.超出范围的值应使用自定义热图进行着色。

In particular:尤其是:

  • Values that fall within the range defined for each column should not be colorized, plain black text on white background cell.落在为每列定义的范围内的值不应在白色背景单元格上着色,纯黑色文本。
  • Values lower than min for that column should be colorized, for example in blue.该列低于min的值应着色,例如蓝色。 The lower their values from the min the darker the shade of blue.从最小值开始,它们的值越低,蓝色的阴影越深。
  • Values higher than max for that column should be colorized, for example in red.该列的高于max的值应着色,例如红色。 The higher their values from the max the darker the shade of red.它们的最大值越高,红色的阴影越深。

Q: I wouldn't know how to approach this with a standard heatmap, I'm not even sure I can accomplish this with a heatmap plot.问:我不知道如何使用标准热图来解决这个问题,我什至不确定我是否可以使用热图 plot 来实现这一点。 Any suggestion?有什么建议吗?

As far as I know, a heatmap can only have one scale of values.据我所知,热图只能有一个数值范围。 I would suggest normalizing the data you have in the df dataframe so the values in every column follow:我建议对df dataframe 中的数据进行规范化,以便每列中的值如下:

  • between 0 and 1 if the value is between df_info 's min max如果值介于df_infomin max之间,则介于01之间
  • below 0 if the value is below df_info 's min如果值低于df_infomin ,则低于0
  • above 1 if the value is above df_info 's max如果值高于df_infomax ,则高于1

To normalize your dataframe use:要标准化您的 dataframe 使用:

for col in df:
    df[col] = (df[col] - df_info[col]['min']) / (df_info[col]['max'] - df_info[col]['min'])

Finally, to create the color-coded heatmap use:最后,要创建颜色编码的热图,请使用:

import seaborn as sns
from matplotlib.colors import LinearSegmentedColormap

vmin = df.min().min()
vmax = df.max().max()

colors = [[0, 'darkblue'],
          [- vmin / (vmax - vmin), 'white'],
          [(1 - vmin)/ (vmax - vmin), 'white'],
          [1, 'darkred']]

cmap = LinearSegmentedColormap.from_list('', colors)
sns.heatmap(df, cmap=cmap, vmin=vmin, vmax=vmax)

The additional calculations with vmin and vmax allow a dynamic scaling of the colormap depending on the differences with the minimums and maximums.使用vminvmax的附加计算允许根据最小值和最大值的差异对颜色图进行动态缩放。

Using your input dataframe we have the following heatmap:使用您的输入 dataframe 我们有以下热图: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM