[英]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:尤其是:
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.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 中的数据进行规范化,以便每列中的值如下:
0
and 1
if the value is between df_info
's min
max
df_info
的min
max
之间,则介于0
和1
之间0
if the value is below df_info
's min
df_info
的min
,则低于0
1
if the value is above df_info
's max
df_info
的max
,则高于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.使用
vmin
和vmax
的附加计算允许根据最小值和最大值的差异对颜色图进行动态缩放。
Using your input dataframe we have the following heatmap:使用您的输入 dataframe 我们有以下热图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.