gpt4 book ai didi

python - 如何在python中为热图的特定元素添加颜色边框或类似的突出显示?

转载 作者:行者123 更新时间:2023-12-04 02:34:22 25 4
gpt4 key购买 nike

我想突出显示热图中的特定元素(在热图中的行和列)
他们是一种在两个轴上突出显示特定元素的方法。
期望的结果应该看起来像向量元素在两个轴上都突出显示的地方。
或类似于下面给出的示例。
我正在使用 seaborn 和 matplotlib。
enter image description here
编辑 1:
我正在使用树状图,这是我的代码,其中 **PairWise ** 表示我的数据矩阵。

import seaborn as sns
%matplotlib inline
pairwise_corr=PairWise.corr(method="spearman")
sns.clustermap(pairwise_corr,method="complete",annot=True,linewidth=0.5)
编辑 2:
这是列作为 b c d e 向量的数据
array([[191, 395,  63,  89, 247, 201],
[379, 121, 187, 430, 265, 478],
[ 38, 220, 406, 450, 349, 354],
[165, 172, 469, 355, 56, 425],
[482, 143, 411, 322, 184, 492],
[ 16, 157, 172, 253, 320, 391],
[328, 443, 418, 392, 95, 93],
[208, 50, 362, 407, 43, 139],
[ 68, 257, 447, 472, 357, 11],
[164, 123, 469, 93, 296, 5],
[ 54, 419, 264, 370, 474, 387],
[171, 137, 108, 174, 339, 481],
[470, 87, 139, 465, 189, 367],
[115, 129, 237, 215, 19, 109],
[196, 462, 256, 125, 329, 379],
[241, 275, 472, 128, 176, 320],
[394, 330, 262, 169, 491, 99],
[406, 182, 190, 404, 71, 221],
[143, 161, 69, 156, 319, 28],
[ 95, 157, 52, 74, 380, 160],
[180, 70, 4, 477, 392, 465],
[406, 211, 116, 68, 324, 288],
[477, 182, 459, 381, 244, 466],
[337, 47, 383, 113, 124, 416],
[386, 202, 14, 129, 325, 209],
[444, 217, 39, 320, 474, 390],
[ 66, 258, 241, 149, 80, 496],
[ 50, 415, 410, 223, 429, 21],
[203, 104, 148, 56, 378, 75],
[277, 254, 172, 130, 142, 59],
[266, 140, 389, 154, 207, 452],
[ 76, 444, 25, 357, 30, 255],
[421, 499, 322, 347, 44, 189],
[142, 136, 50, 445, 234, 404],
[487, 478, 282, 290, 172, 96],
[ 75, 99, 82, 13, 148, 424],
[ 61, 73, 181, 363, 96, 406],
[ 71, 74, 490, 354, 364, 62],
[409, 76, 475, 491, 267, 398],
[422, 72, 272, 136, 140, 301],
[128, 55, 100, 236, 255, 499],
[ 13, 240, 254, 273, 59, 262],
[ 45, 474, 153, 132, 422, 391],
[264, 25, 382, 464, 440, 11],
[120, 32, 497, 321, 81, 343],
[165, 328, 284, 447, 279, 317],
[ 1, 247, 273, 145, 254, 47],
[107, 135, 222, 140, 221, 207],
[319, 387, 426, 86, 344, 481],
[447, 344, 139, 336, 191, 442],
[112, 173, 249, 371, 364, 246],
[403, 20, 378, 387, 492, 308],
[ 58, 206, 419, 378, 27, 487],
[167, 249, 340, 253, 389, 38],
[334, 27, 45, 112, 298, 359],
[498, 233, 72, 111, 304, 117],
[464, 285, 119, 433, 362, 7],
[374, 36, 101, 21, 141, 430],
[322, 80, 53, 184, 467, 330],
[236, 461, 6, 353, 80, 193],
[342, 178, 372, 227, 51, 460],
[179, 448, 51, 309, 87, 403],
[253, 329, 81, 28, 428, 490],
[156, 128, 201, 74, 71, 418],
[436, 393, 128, 370, 76, 115],
[359, 157, 111, 425, 28, 92],
[116, 377, 110, 37, 348, 329],
[355, 282, 200, 205, 301, 198],
[180, 390, 80, 183, 248, 116],
[340, 113, 79, 479, 218, 310],
[178, 334, 126, 470, 13, 405],
[450, 50, 141, 145, 153, 108],
[483, 120, 124, 94, 475, 259],
[191, 366, 456, 400, 390, 378],
[292, 245, 403, 321, 195, 138],
[213, 474, 62, 351, 136, 38],
[372, 314, 412, 191, 30, 244],
[ 78, 473, 100, 448, 36, 19],
[274, 78, 491, 29, 193, 243],
[ 13, 177, 31, 471, 44, 488],
[160, 310, 9, 43, 481, 498],
[219, 465, 486, 91, 460, 429],
[ 79, 326, 154, 337, 45, 8],
[402, 124, 242, 479, 438, 170],
[206, 354, 348, 133, 389, 0],
[ 57, 239, 31, 42, 157, 458],
[392, 71, 96, 134, 80, 436],
[233, 268, 193, 129, 70, 385],
[486, 111, 328, 219, 353, 371],
[303, 476, 352, 30, 490, 428],
[496, 59, 340, 332, 401, 389],
[ 66, 302, 250, 322, 131, 328],
[362, 407, 118, 237, 104, 72],
[192, 493, 1, 42, 301, 103],
[352, 402, 123, 435, 209, 314],
[216, 42, 485, 72, 392, 251],
[323, 36, 498, 9, 120, 327],
[207, 178, 495, 144, 102, 276],
[179, 476, 24, 51, 154, 69],
[ 19, 62, 450, 199, 382, 139]])
任何资源或引用资料都会有所帮助。
谢谢

最佳答案

您可以添加所需大小的矩形。位置 (0, 1, 2, ...) 取决于要突出显示的标签的索引。设置 clip_on=False还有助于显示主情节之外的部分线条。

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
import seaborn as sns
import numpy as np

labels = list('abcdef')
N = len(labels)
ax = sns.heatmap(np.random.uniform(0, 1, (N, N)), cmap='summer', annot=True, linewidths=.5,
xticklabels=labels, yticklabels=labels)
wanted_label = 'c'
wanted_index = labels.index(wanted_label)
x, y, w, h = 0, wanted_index, N, 1
for _ in range(2):
ax.add_patch(Rectangle((x, y), w, h, fill=False, edgecolor='crimson', lw=4, clip_on=False))
x, y = y, x # exchange the roles of x and y
w, h = h, w # exchange the roles of w and h
ax.tick_params(length=0)
plt.show()
resulting plot
将此方法纳入 clustermap :
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
import seaborn as sns; sns.set()
import numpy as np
import pandas as pd

data = np.random.uniform(0, 1, size=(20, 6))
# data = np.array([[...]])
columns = list('abcdef')
N = len(columns)
PairWise = pd.DataFrame(columns=columns, data=data)
pairwise_corr = PairWise.corr(method="spearman")
g = sns.clustermap(pairwise_corr, method="complete", annot=True, linewidth=0.5)

wanted_label = 'f'
# wanted_row = np.where(np.array(columns)[g.dendrogram_row.reordered_ind] == wanted_label)[0]
# wanted_col = np.where(np.array(columns)[g.dendrogram_col.reordered_ind] == wanted_label)[0]
wanted_row = g.dendrogram_row.reordered_ind.index(columns.index(wanted_label))
wanted_col = g.dendrogram_col.reordered_ind.index(columns.index(wanted_label))

xywh_row = (0, wanted_row, N, 1)
xywh_col = (wanted_col, 0, 1, N)
for x, y, w, h in (xywh_row, xywh_col):
g.ax_heatmap.add_patch(Rectangle((x, y), w, h, fill=False, edgecolor='yellow', lw=4, clip_on=False))
g.ax_heatmap.tick_params(length=0)
plt.show()
clustermap plot

关于python - 如何在python中为热图的特定元素添加颜色边框或类似的突出显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62533046/

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