如何在python中为热图的特定元素添加颜色边框或类似的高光?
我想突出显示热图中的特定元素(在热图的行和列上) 是一种在两个轴上突出显示特定元素的方法 期望的结果应该类似于矢量元素在两个轴上高亮显示的位置。 或类似于下面给出的示例 我正在使用seaborn和matplotlib 编辑1: 我使用的是dendogram,这是我的代码,其中**成对**表示我的数据矩阵如何在python中为热图的特定元素添加颜色边框或类似的高光?,python,python-3.x,matplotlib,seaborn,heatmap,Python,Python 3.x,Matplotlib,Seaborn,Heatmap,我想突出显示热图中的特定元素(在热图的行和列上) 是一种在两个轴上突出显示特定元素的方法 期望的结果应该类似于矢量元素在两个轴上高亮显示的位置。 或类似于下面给出的示例 我正在使用seaborn和matplotlib 编辑1: 我使用的是dendogram,这是我的代码,其中**成对**表示我的数据矩阵 import seaborn as sns %matplotlib inline pairwise_corr=PairWise.corr(method="spearman"
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
也有助于显示主绘图外的线条部分
从matplotlib导入pyplot作为plt
从matplotlib.patches导入矩形
导入seaborn作为sns
将numpy作为np导入
标签=列表('abcdef')
N=长度(标签)
ax=sns.heatmap(np.random.uniform(0,1,(N,N)),cmap='summer',annot=True,线宽=0.5,
xticklabels=标签,yticklabels=标签)
通缉犯标签='c'
通缉索引=标签。索引(通缉标签)
x、 y,w,h=0,N,1
对于范围(2)内的uu:
ax.add_patch(矩形((x,y),w,h,fill=False,edgecolor='crimson',lw=4,clip_-on=False))
x、 y=y,x#交换x和y的角色
w、 h=h,w#交换w和h的角色
最大刻度参数(长度=0)
plt.show()
要将此方法纳入到计划中,请执行以下操作:
从matplotlib导入pyplot作为plt
从matplotlib.patches导入矩形
作为sns导入seaborn;sns.set()
将numpy作为np导入
作为pd进口熊猫
数据=np.随机.均匀(0,1,大小=(20,6))
#data=np.array([…])
columns=list('abcdef')
N=len(列)
成对=pd.DataFrame(列=列,数据=数据)
两两对比=两两对比(方法=“斯皮尔曼”)
g=sns.clustermap(成对,method=“complete”,annot=True,线宽=0.5)
通缉犯标签='f'
#想要的行=np.where(np.array(columns)[g.dendrogram\u row.reordered\u ind]==想要的标签)[0]
#想要的列=np.where(np.array(columns)[g.dendrogram\u col.reordered\u ind]==想要的列[0]
想要的行=g.树状图\行.重新排序的索引(列.索引(想要的标签))
通缉目录=g.树状图目录重新排序索引(列索引(通缉标签))
xywh_行=(0,通缉_行,N,1)
xywh_列=(通缉_列,0,1,N)
对于x、y、w、h英寸(xywh_行、xywh_列):
g、 ax\u热图。添加补丁(矩形((x,y),w,h,fill=False,edgecolor='yellow',lw=4,clip\u on=False))
g、 ax\u热图。勾选参数(长度=0)
plt.show()
这个答案是否就是您想要的答案?我已经看过了这个答案,但它不适用于整行/整列。顺便说一句…感谢您的参考,请稍候…这个解决方案很好,但在执行群集dendogram后我遇到了问题,因为在那之后索引发生了变化…请帮助我用代码更新了我的问题。如果你想要更多的信息,我会提供。另外,热图是代码的一部分,将被重新生成,索引也是如此……所以我正在寻找作为模板工作的解决方案