gpt4 book ai didi

python - 基于行值的seaborn热图配色方案

转载 作者:行者123 更新时间:2023-12-04 13:57:22 28 4
gpt4 key购买 nike

我有一个数据框,部分复制如下:

import pandas as pd
import numpy as np
tab = pd.DataFrame(np.array([[ 46, 39, 25, 29, 21, 12, 33, 32, 70, 109, 144, 158, 161,
184, 163, 113, 117, 82, 76, 88, 77, 76, 64, 35],
[ 39, 33, 29, 29, 26, 14, 25, 33, 60, 83, 126, 117, 111,
148, 141, 104, 92, 75, 78, 74, 63, 67, 52, 39],
[ 30, 27, 14, 11, 20, 17, 21, 31, 48, 62, 83, 78, 88,
90, 80, 67, 53, 61, 47, 54, 50, 48, 35, 26],
[ 30, 24, 19, 15, 17, 10, 12, 18, 34, 69, 88, 79, 109,
95, 89, 82, 53, 46, 53, 57, 39, 41, 26, 29],
[ 37, 31, 18, 12, 30, 13, 15, 19, 51, 61, 74, 81, 77,
100, 96, 74, 60, 57, 42, 48, 43, 40, 29, 25],
[ 14, 8, 14, 11, 13, 7, 9, 15, 42, 49, 50, 44, 53,
42, 31, 31, 30, 27, 33, 25, 27, 17, 20, 17],
[ 10, 15, 6, 10, 15, 11, 7, 18, 28, 43, 49, 37, 41,
33, 37, 32, 26, 28, 19, 24, 19, 19, 13, 18],
[ 9, 9, 8, 12, 7, 11, 4, 8, 14, 15, 23, 30, 29,
34, 25, 39, 22, 20, 15, 23, 12, 19, 14, 13],
[ 0, 3, 4, 1, 1, 0, 3, 4, 4, 5, 3, 5, 6,
7, 3, 3, 6, 4, 2, 3, 3, 2, 2, 2],
[ 3, 0, 1, 0, 0, 0, 1, 1, 4, 8, 2, 4, 7,
2, 2, 9, 3, 5, 1, 5, 2, 0, 4, 1]]), index =
['Stadsdeel Zuid', 'Stadsdeel West', 'Stadsdeel Nieuw-West',
'Stadsdeel Centrum', 'Stadsdeel Oost', 'Stadsdeel Noord',
'Wijk 00 Amstelveen', 'Stadsdeel Zuidoost', 'Wijk 00',
'Wijk 00 Aalsmeer'])

我就这样创建了一个热图
ax = sns.heatmap(tab, linewidths=.5 ,robust=True ,annot_kws = {'size':14})
ax.tick_params(labelsize=14)
ax.figure.set_size_inches((12, 10))

我希望 anchor 定颜色图的值基于每行的最小-最大值 ,以便具有较低值的行也很明显。 (实际上,该表包含更多的低值行,而热图几乎无法按颜色显示)

如何实现呢?

enter image description here

最佳答案

我用每行中的最大值将tab行归一化:

tab_n = tab.div(tab.max(axis=1), axis=0)

其中 tab_n是规范化标签,其值在[0,1]范围内。希望能有所帮助。绘制 tab_n应该返回如下热图:

enter image description here

关于python - 基于行值的seaborn热图配色方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42111075/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com